Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

This chapter is from the book

This chapter is from the book

2.6 The Bash Shell Constructs

The Korn and Bash shells are very similar, but there are some differences. The Bash constructs are listed in Table 2.4.

Table 2.4. Bash Shell Syntax and Constructs

The shbang line

The "shbang" line is the very first line of the script and lets the kernel know what shell will be interpreting the lines in the script. The shbang line consists of a #! followed by the full pathname to the shell, and can be followed by options to control the behavior of the shell.

EXAMPLE

#!/bin/bash

Comments

Comments are descriptive material preceded by a # sign. They are in effect until the end of a line and can be started anywhere on the line.

EXAMPLE

# This is a comment

Wildcards

There are some characters that are evaluated by the shell in a special way. They are called shell metacharacters or "wildcards." These characters are neither numbers or letters. For example, the *, ?, and [ ] are used for filename expansion. The <, >, 2>, >>, and | symbols are used for standard I/O redirection and pipes. To prevent these characters from being interpreted by the shell they must be quoted.

EXAMPLE


rm *; ls ??; cat file[1-3];
echo "How are you?"

Displaying output

To print output to the screen, the echo command is used. Wildcards must be escaped with either a backslash or matching quotes.

EXAMPLE

echo "How are you?"

Local variables

Local variables are in scope for the current shell. When a script ends, they are no longer available; i.e., they go out of scope. Local variables can also be defined with the built-in declare function. Local variables are set and assigned values.

EXAMPLE


variable_name=value
declare variable_name=value

name="John Doe"
x=5

Global variables

Global variables are called environment variables and are created with the export built-in command. They are set for the currently running shell and any process spawned from that shell. They go out of scope when the script ends.

The built-in declare function with the -x option also sets an environment variable and marks it for export.

EXAMPLE


export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.

Extracting values from variables

To extract the value from variables, a dollar sign is used.

EXAMPLE


echo $variable_name
echo $name
echo $PATH

Reading user input

The user will be asked to enter input. The read command is used to accept a line of input. Multiple arguments to read will cause a line to be broken into words, and each word will be assigned to the named variable.

EXAMPLE


echo "What is your name?"
read name
read name1 name2 ...

Arguments

Arguments can be passed to a script from the command line. Positional parameters are used to receive their values from within the script.

EXAMPLE

At the command line:

$ scriptname arg1 arg2 arg3 ...

In a script:

echo $1 $2 $3  Positional parameters

echo $*                All the positional paramters

echo $#                The number of positional parameters

Arrays

The Bourne shell utilizes positional parameters to create a word list. In addition to positional parameters, the Bash shell supports an array syntax whereby the elements are accessed with a subscript, starting at 0. Bash shell arrays are created with the declare -a command.

EXAMPLE


set apples pears peaches (positional parameters)
echo $1 $2 $3

declare -a array_name=(word1 word2 word3 ...)
declare -a fruit=( apples pears plums )
echo ${fruit[0]}

Command substitution

Like the C/TC shells and the Bourne shell, the output of a UNIX/Linux command can be assigned to a variable, or used as the output of a command in a string, by enclosing the command in backquotes. The Bash shell also provides a new syntax. Instead of placing the command between backquotes, it is enclosed in a set of parentheses, preceded by a dollar sign.

EXAMPLE


variable_name=`command`
variable_name=$( command )
echo $variable_name

echo "Today is `date`"
echo "Today is $(date)"

Arithmetic

The Bash shells support integer arithmetic. The declare -i command will declare an integer type variable.The Korn shell's typeset command can also be use for backward compatibility. Integer arithmetic can be performed on variables declared this way. Otherwise the (( )) (let command ) syntax is used for arithmetic operations.

EXAMPLE

declare -i variable_name    used for bash

typeset -i variable_name    can be used to be compatible with ksh

(( n=5 + 5 ))
echo $n

Operators

The Bash shell uses the built-in test command operators to test numbers and strings, similar to C language operators.

EXAMPLE

Equality: Logical:
==   equal to   &&   and
!=   not equal to   ||  or
    !  not
Relational:  
>   greater than  
>=   greater than, equal to  
<   less than  
<=   less than, equal to  
   

 

Conditional statements

The if construct is followed by an expression enclosed in parentheses. The operators are similar to C operators. The then keyword is placed after the closing paren. An if must end with an endif. The new [[ ]] test command is now used to allow pattern matching in conditional expressions. The old [ ] test command is still available for backward compatibility with the Bourne shell. The case command is an alternative to if/else.

EXAMPLE

The if construct is:

if  command
then
   block of statements
fi

if  [[ expression  ]]
then
   block of statements
fi

if  (( numeric expression  ))
then
   block of statements
else
   block of statements
 fi

The if/else construct is:

if  command
then
   block of statements
else
   block of statements
fi

if  [[ expression ]]
then
   block of statements
else
   block of statements
fi

if  ((  numeric expression ))
then
   block of statements
else
   block of statements
fi

The case construct is:

case variable_name in
   pattern1)
      statements
         ;;
   pattern2)
      statements
         ;;
   pattern3)
         ;;
esac

case "$color" in
   blue)
      echo $color is blue
         ;;
   green)
      echo $color is green
         ;;
   red|orange)
      echo $color is red or orange
         ;;
   *) echo "Not a matach"
         ;;
esac

The if/else/else if construct is:

if  command
then
   block of statements
elif  command
then
   block of statements
else if  command
then
   block of statements
else
   block of statements
fi
-------------------------
if  [[ expression ]]
then
   block of statements
elif  [[  expression ]]
then
   block of statements
else if  [[  expression ]]
then
   block of statements
else
   block of statements
fi
--------------------------
if  ((  numeric expression ))
then
   block of statements
elif  ((  numeric expression ))
then
   block of statements
else if  ((numeric expression))
then
   block of statements
else
   block of statements
fi

Loops

There are four types of loops: while, until, for, and select.

The while loop is followed by an expression enclosed in square brackets, a do keyword, a block of statements, and terminated with the done keyword. As long as the expression is true, the body of statements between do and done will be executed. The compound test operator [[ ]] is new with Bash, and the old-style test operator [ ] can still be used to evaluate conditional expressions for backward compatibility with the Bourne shell.

The until loop is just like the while loop, except the body of the loop will be executed as long as the expression is false.

The for loop is used to iterate through a list of words, processing a word and then shifting it off, to process the next word. When all words have been shifted from the list, it ends. The for loop is followed by a variable name, the in keyword, a list of words, then a block of statements, and terminates with the done keyword.

The select loop is used to provide a prompt and a menu of numbered items from which the user inputs a selection. The input will be stored in the special built-in REPLY variable. The select loop is normally used with the case command.

The loop control commands are break and continue. The break command allows control to exit the loop before reaching the end of it, and the continue command allows control to return to the looping expression before reaching the end.

EXAMPLE

while command                                until
ccc.gif command
               do                                           do
               block of statements                         
               ccc.gif block of statements
               done                                         done
               -------------------------                   
               ccc.gif ---------------------------
               while [[ string expression ]]                until
               ccc.gif [[ string expression ]]
               do                                           do
               block of statements                       block
               ccc.gif of statements
               done                                         done
               -------------------------                   
               ccc.gif ----------------------------
               while (( numeric expression ))               until
               ccc.gif (( numeric expression ))
               do                                           do
               block of statements                         
               ccc.gif block of statements
               done                                         done
               
               for variable in word_list                   
               ccc.gif select variable in word_list
               do                                           do
               block of statements                       block
               ccc.gif of statements
               done                                         done
               --------------------------                  
               ccc.gif ----------------------------
               for color in red green b                    
               ccc.gif PS3="Select an item from the menu"
               do                                           do
               ccc.gif item in blue red green
               echo $color                               echo
               ccc.gif $item
               done                                         done

Shows menu:

  1. blue

  2. red

  3. green

Functions

Functions allow you to define a section of shell code and give it a name. There are two formats, one from the Bourne shell, and the Bash version that uses the function keyword.

EXAMPLE

function_name() {
   block of code
}

function  function_name {
   block of code
}
------------------------
function  lister {
   echo Your present working directory is `pwd`
   echo Your files are:
   ls
}

2.6.1 The Bash Shell Script

Example 2.7.

1   #!/bin/bash
    # GNU bash versions 2.x
   2   # The Party Program––Invitations to friends from the "guest" file
   3   guestfile=~/shell/guests
   4   if [[ ! –e "$guestfile" ]]
   then
   5       printf "${guestfile##*/} non–existent"
   exit 1
   fi
   6   export PLACE="Sarotini's"
   7   (( Time=$(date +%H) + 1 ))
   8   declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
   9   declare -i  n=0
   10  for person in $(cat $guestfile)
   do
   11      if  [[ $person == root ]]
   then
   continue
   else
   # Start of here document
   12            mail –v –s "Party" $person <<- FINIS
   Hi $person! Please join me at $PLACE for a party!
   Meet me at $Time o'clock.
   I'll bring the ice cream. Would you please bring
   ${foods[$n] and anything else you would like to eat?
   Let me know if you can make it.
   Hope to see you soon.
   Your pal,
   ellie@$(hostname)
   FINIS
   13            n=n+1
   14            if (( ${#foods[*]} ==  $n ))
   then
   15               declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`
   sandwiches)
   16            n=0
   fi
   fi
   17  done
   printf "Bye..."
   

EXPLANATION

  1. This line lets the kernel know that you are running a Bash shell script.

  2. This is a comment. It is ignored by the shell, but important for anyone trying to understand what the script is doing.

  3. The variable guestfile is set to the full pathname of a file called guests.

  4. This line reads: If the file guests does not exist, then print to the screen "guests nonexistent" and exit from the script.

  5. The built-in printf function dipslays only the filename (pattern matching) and the string "non-existent".

  6. An environment (global) variable is assigned and exported.

  7. A numeric expression uses the output of the UNIX/Linux date command to get the current hour. The hour is assigned to the variable, Time.

  8. A Bash array, foods, is defined (declare –a) with a list of elements.

  9. An integer, n, is defined with an initial value of zero.

  10. For each person on the guest list, except the user root, a mail message will be created inviting the person to a party at a given place and time, and assigning a food from the list to bring.

  11. If the value in $person is root, control goes back to the top of the for loop and starts at the next person on the list.

  12. The mail message is sent. The message body is contained in a here document.

  13. The integer, n, is incremented by 1.

  14. If the number of foods is equal to the value of the last number in the array index, the list is empty.

  15. The array called foods is reassigned values. After a message has been sent, the food list is shifted so that the next person will get the next food on the list. If there are more people than foods, the food list will be reset, ensuring that each person is assigned a food.

  16. The variable n, which will serve as the array index, is reset back to zero.

  17. This marks the end of the looping statements.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020