Linux+ Guide to Linux Certification
Chapter Eight
Working with the BASH Shell
ObjectivesObjectives
• Redirect the input and output of a command• Identify and manipulate common shell environment
variables• Create and export new shell variables• Edit environment files to create variables upon shell
startup• Describe the purpose and nature of shell scripts• Create and execute basic shell scripts• Effectively use common decision constructs in shell
scripts
Command Input and OutputCommand Input and Output
• The BASH shell is responsible for providing a user interface and interpreting commands entered on the command line
• The BASH shell can also manipulate command input and output, provided the user specifies certain shell metacharacters on the command line alongside the command
• File descriptors– Numeric labels used to define command input and
command output
Command Input and OutputCommand Input and Output
• There are three file descriptors available to each command that can be manipulated by the BASH shell:– Standard Input (stdin)– Standard Output (stdout)– Standard Error (stderror)
Command Input and OutputCommand Input and Output
• Standard Input (stdin)– Represent information inputed to a command
during execution
• Standard Output (stdout)– Represents the desired output from a command
• Standard Error (stderror)– Represents any error message generated by a
command
Command Input and OutputCommand Input and Output
Figure 8-1:The three common file descriptors
RedirectionRedirection
• The BASH shell can redirect Standard Output and Standard Error from the terminal screen to a file on the filesystem using the “>” shell metacharacter followed by absolute or relative pathname of the file
• You can redirect both Standard Output and Standard Error to separate files at the same time
• It is important to use separate filenames to hold the contents of Standard Output and Standard Error
RedirectionRedirection
Table 8-1: Common redirection examples
RedirectionRedirection
Table 8-1 (continued): Common redirection examples
PipesPipes
• Redirection only occurs from a command to a file and vice versa
• You may also send the Standard Output of one command to another command as Standard Input
• Pipe– A string of commands connected by “|”
metacharacters
PipesPipes
Figure 8-2: Piping information from one command to another
PipesPipes
Figure 8-3: Piping several commands
PipesPipes
Table 8-2: Common filter commands
Shell VariablesShell Variables
• A BASH shell has several variables in memory at any one time– A variable is simply a reserved portion of memory
containing information that may be accessed
• Most variables in the shell are referred to as environment variables since they are typically set by the system and contain information that the system and programs access regularly
Shell VariablesShell Variables
• You may create your own custom variables– These variables are called user-defined
variables
• In addition to environment and user-defined variables, there are special variables that are useful when executing commands and creating new files and directories
Environment VariablesEnvironment Variables
• There are many environment variables
• Environment variables are set by default in the BASH shell
• To see a list of these variables and their current values, you may use the set command
Environment VariablesEnvironment Variables
Table 8-3: Common BASH environment variables
Environment VariablesEnvironment Variables
Table 8-3 (continued): Common BASH environment variables
Environment VariablesEnvironment Variables
Table 8-3 (continued): Common BASH environment variables
User-Defined VariablesUser-Defined Variables
• Variable identifier– The name of a variable
• When creating new variables, it is important to note the following features of variable identifiers:– They can contain alphanumeric characters, the dash
character, or the underscore character– They must not start with a number– They are typically capitalized to follow convention
User-Defined VariablesUser-Defined Variables
• Most commands that are run by the shell are run in a separate subshell– The subshell is created by the current shell
– Variables created in the current shell are not available to those subshells and the commands running within them
Other VariablesOther Variables
• There are other variables that are not displayed by the set or env commands– These variables perform specialized functions in
the shell
• The UMASK variable used earlier in this textbook us an example of a special variable that performs a special function in the BASH shell and must be set by the umask command
Environment FilesEnvironment Files
• When you exit your BASH shell, all variables stored in memory are destroyed along with the shell itself
• Thus to ensure that variables are accessible to a shell at all times, you must place variables in a file that is executed each time you log in and start a BASH shell– These files are called environment files
Environment FilesEnvironment Files
• Some common BASH shell environment files and the order they are executed in are listed below:– /etc/profile– ~/.bash_profile– ~/.bash_login– ~/.profile
Shell ScriptsShell Scripts
• Shell script– Text files that contain a list of commands or
constructs for the shell to execute in order
• Hashpling– The first line in a shell script, which defines the
shell that will be used to interpret the commands in the script file
Escape SequencesEscape Sequences
• Escape sequences– Character sequences that have special meaning inside the echo
commandTable 8-4: Common echo sequences
Escape SequencesEscape Sequences
Table 8-4: Common echo sequences
Reading Standard InputReading Standard Input
• There may be times where a shell script may need input from the user executing the program– This input may then be stored in a variable for
later use
• The read command takes user input from Standard Input and places it in a variable specified by an argument to the read command
Decision ConstructsDecision Constructs
Figure 8-4:A sample decision construct
Decision ConstructsDecision Constructs
Figure 8-5:A sample decision construct
The if ConstructThe if Construct
• Common rules that govern if constructs:– elif (else if) and else statements are optional– You may have an unlimited number of elif
statements– The do these commands section may consist of
multiple commands, one per line
The if ConstructThe if Construct
• Common rules that govern if constructs (continued):– The do these commands section is typically
indented from the left hand side of the text file for readability, but does not need to be
– The end of the statement must be a backwards “if” fi
– The this is true of the if syntax seen earlier may be a command or a test statement
The if ConstructThe if Construct
Table 8-5: Common test statements
The if ConstructThe if Construct
Table 8-5 (continued): Common test statements
The if ConstructThe if Construct
Table 8-6: Special operators in test statements
The case ConstructThe case Construct
• The case statement compares the value of a variable with several different patterns of text or numbers
• If there is a match, then the commands to the right of the pattern will be executed
• As with the if construct, the case construct must be ended by a backwards “case” (esac)
The && and || ConstructsThe && and || Constructs
• Although the if and case constructs are versatile, there are some shortcut constructs that take less time when only one decision needs to be made during the execution of a program– These constructs are && and ||
– The syntax of these constructs is listed below:• command && command
• command || command
Chapter SummaryChapter Summary
• There are three components available to commands• Standard Input is typically user input taken from the
keyboard• Standard Output and Standard Error are sent to the
terminal screen by default• You may redirect the Standard Output and Standard
Error of a command to a file using redirection symbols
Chapter SummaryChapter Summary
• To redirect the Standard Output from one command to the Standard Input of another, you must use the pipe symbol
• Most variables available to the BASH shell are environment variables, which are loaded into memory after login from environment files
• You may create your own variables in the BASH shell and export them such that they are available to programs started by the shell
Chapter SummaryChapter Summary
• The UMASK variable and command aliases are special variables that must be set using a certain command
• Shell scripts can be used to execute several Linux commands
• Decision constructs may be used within shell scripts to execute certain Linux commands based on user input or the results of a certain command
Top Related