CONTENTS |
With an interactive shell, the standard input, output, and error are tied to a terminal. When using the Bourne Again (bash) shell interactively, you type UNIX commands at the bash prompt and wait for a response. Bash provides you with a large assortment of built-in commands and command line shortcuts, such as history, aliases, file and command completion, command line editing, and many more. Some of the features were present in the standard UNIX Bourne shell but the GNU project has expanded the shell to include a number of new features as well adding POSIX compliancy. With the release of bash 2.x, so many features of the UNIX Korn shell and C shell have been included that the bash shell is a fully functional shell at both the interactive and programming level, while upwardly compatible with the standard Bourne shell. For UNIX users, bash offers an alternative to the standard shells, sh, csh, and ksh.[1]
This chapter focuses on how you interact with bash at the command line and how to customize your working environment. You will learn how to take advantage of all shortcuts and built-in features in order to create an efficient and fun working environment. The next chapter takes you a step further. Then you will be ready to write bash shell scripts to further tailor the working environment for yourself by automating everyday tasks and developing sophisticated scripts, and if you are an administrator, doing the same not only for yourself but also for whole groups of users.
The Bourne Again shell is a Capricorn, born on January 10, 1988, fathered by Brian Fox and later adopted by Chet Ramey, who now officially maintains bash, enhances it, and fixes bugs. The first version of bash was 0.99. The current version (as of this writing) is version 2.04. Major enhancements were completed in version 2.0, but there are a num ber of operating systems that are still using version 1.14.7. All versions are freely available under the GNU public license. To see what version you are using, use the - -version option to bash or print the value of the BASH_VERSION environment variable.
$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) Copyright 1998 Free Software Foundation, Inc. $ echo $BASH_VERSION 2.03.0(1)-release
If the bash shell is your login shell, it follows a chain of processes before you see a shell prompt.[2]
When the system boots, the first process to run is called init, PID #1. It spawns a getty process. This process opens up the terminal ports, providing a place where standard input comes from and a place where standard output and errors go, and puts a login prompt on your screen. The /bin/login program is then executed. The login program prompts for a password, encrypts and verifies the password, sets up an initial environment, and starts up the login shell, /bin/bash, the last entry in the passwd file. The bash process looks for the system file, /etc/profile, and executes its commands. It then looks in the user's home directory for an initialization file called .bash_profile. After executing commands from bash _profile, [3] it will execute a command from the user's ENV file, usually called .bashrc, and finally the default dollar sign ($) prompt appears on your screen and the shell waits for commands. (For more on initialization files, see "The Environment".)
Changing the Shell at the Command Line. If you want to start another shell from the command line temporarily (without changing the /etc/passwd file), just type the name of the shell. For example, if you are currently using the standard Bourne shell and would rather have bash as your shell, you can change the shell at the command line simply by typing bash.
1 $ ps PID TTY TIME CMD 1574 pts/6 0:00 sh 2 $ bash bash-2.03$ 3 bash-2.03$ ps PID TTY TIME CMD 1574 pts/6 0:00 sh 1576 pts/6 0:00 bash
EXPLANATION
|
The environment of a process consists of variables, open files, the current working directory, functions, resource limits, signals, and so forth. It defines those features that are inherited from one shell to the next and the configuration for the working environment. The configuration for the user's shell is defined in the shell initialization files.
The Initialization Files. The bash shell has a number of startup files that are sourced. Sourcing a file causes all settings in the file to become part of the current shell; i.e., a subshell is not created. (The source command is discussed in "The source or dot Command" on page 621.) The initialization files are sourced depending on the whether the shell is a login shell, an interactive shell (but not the login shell), or a noninteractive shell (a shell script).
When you log on, before the shell prompt appears, the systemwide initialization file, etc/profile, is sourced. Next, if it exists, the .bash_profile in the user's home directory is sourced. It sets the user's aliases and functions and then sets user-specific environment variables and startup scripts.
If the user doesn't have a .bash_profile, but does have a file called .bash_login, that file will be sourced, and if he doesn't have a .bash_login, but does have a .profile, it will be sourced. (The .bash_login file is similar to the C shell's .login file and the .profile is normally sourced by the Bourne shell when it starts up.)
Here is a summary of the order that bash processes its initialization files [4] (see figure 11.2):
if /etc/profile exists, source it, if ~/.bash_profile exists, source it, if ~/.bashrc exists, source it, else if ~/.bash_login exists, source it, else if ~/.profile exists, source it.
The /etc/profile File. The /etc/profile file is a systemwide initialization file set up by the system administrator to perform tasks when the user logs on. It is executed when the bash shell starts up. It is also available to all Bourne and Korn shell users on the system and normally performs such tasks as checking the mail spooler for new mail and displaying the message of the day from the /etc/motd file. (The following examples will make more sense after you have completed this chapter.)
(Sample /etc/profile) # /etc/profile # Systemwide environment and startup programs # Functions and aliases go in /etc/bashrc 1 PATH="$PATH:/usr/X11R6/bin" 2 PS1="[\u@\h \W]\\$ " 3 ulimit -c 1000000 4 if [ 'id -gn' = 'id -un' -a 'id -u' -gt 14 ]; then 5 umask 002 else umask 022 fi 6 USER='id -un' 7 LOGNAME=$USER 8 MAIL="/var/spool/mail/$USER" 9 HOSTNAME='/bin/hostname' 10 HISTSIZE=1000 11 HISTFILESIZE=1000 12 export PATH PS1 HOSTNAME HISTSIZE HISTFILESIZE USER LOGNAME MAIL 13 for i in /etc/profile.d/*.sh ; do 14 if [ -x $i ]; then 15 . $i fi 16 done 17 unset i #
EXPLANATION
|
The ~/.bash_profile File. If the ~/.bash_profile is found in the user's home directory, it is sourced after the /etc/profile. If ~/.bash_profile doesn't exist, bash will look for another user-defined file, ~./bash_login, and source it, and if ~./bash_login doesn't exist, it will source the ~/.profile, if it exists. Only one of the three files (~/.bash_profile, ~/.bash_login, or ~/.profile) will be sourced. Bash will also check to see if the user has a .bashrc file and then source it.
(Sample .bash_profile) # .bash_profile # The file is sourced by bash only when the user logs on. # Get the aliases and functions 1 if [ -f ~/.bashrc ]; then 2 . ~/.bashrc fi # User-specific environment and startup programs 3 PATH=$PATH:$HOME/bin 4 ENV=$HOME/.bashrc # or BASH_ENV=$HOME/.bashrc 5 USERNAME="root" 6 export USERNAME ENV PATH 7 mesg n 8 if [ $TERM = linux ] then startx # Start the X Window system fi
EXPLANATION
|
[a] BASH_ENV is used by versions of bash starting at 2.0.
The BASH_ENV (ENV) Variable. Before bash version 2.0, the BASH_ENV file was simply called the ENV file (same as in Korn shell). The BASH_ENV (ENV) variable is set in the ~/.bash_profile. It is assigned the name of a file that will be executed every time an interactive bash shell or bash script is started. The BASH_ENV (ENV) file will contain special bash variables and aliases. The name is conventionally .bashrc, but you can call it anything you want. The BASH_ENV (ENV) file is not processed when the privileged option is on (bash -p or set -o privileged) or the - -norc command line option is used (bash -norc or bash - -norc [bash 2.x +]).
The .bashrc File. The BASH_ENV (ENV) variable is assigned (by convention) the name .bashrc. This file is automatically sourced every time a new or interactive bash shell or bash script starts. It contains settings that pertain only to the bash shell.
(Sample .bashrc) # If the .bashrc file exists, it is in the user's home directory. # It contains aliases (nicknames for commands) and user-defined # functions. # .bashrc # User-specific aliases and functions 1 set -o vi 2 set -o noclobber 3 set -o ignoreeof 4 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' 5 stty erase ^h # Source global definitions 6 if [ -f /etc/bashrc ]; then . /etc/bashrc fi 7 case "$-" in 8 *i*) echo This is an interactive bash shell ;; 9 *) echo This shell is noninteractive ;; esac 10 history_control=ignoredups 11 function cd { builtin cd $1; echo $PWD; }
EXPLANATION
|
The /etc/bashrc File. Systemwide functions and aliases can be set in the /etc/bashrc file. The primary prompt is often set here.
(Sample /etc/bashrc) # Systemwide functions and aliases # Environment stuff goes in /etc/profile # For some unknown reason bash refuses to inherit # PS1 in some circumstances that I can't figure out. # Putting PS1 here ensures that it gets loaded every time. 1 PS1="[\u@\h \W]\\$ " 2 alias which="type -path"
EXPLANATION
|
The ~/.profile File. The .profile file is a user-defined initialization file. It is found in the user's home directory, and sourced once at login if running sh (Bourne shell). If running bash, .profile will be run if bash cannot find any other of the initialization files listed above. It allows a user to customize and modify his shell environment. Environment and terminal settings are normally put here, and if a window application or database application is to be initiated, it is started here.
(Sample .profile) # A login initialization file sourced when running as sh or the #.bash_profile or .bash_login are not found. 1 TERM=xterm 2 HOSTNAME='uname -n' 3 EDITOR=/bin/vi 4 PATH=/bin:/usr/ucb:/usr/bin:/usr/local:/etc:/bin:/usr/bin:. 5 PS1="$HOSTNAME $ > " 6 export TERM HOSTNAME EDITOR PATH PS1 7 stty erase ^h 8 go () { cd $1; PS1='pwd'; PS1='basename $PS1'; } 9 trap '$HOME/.logout' EXIT 10 clear
EXPLANATION
|
The ~/.bash-logout File. When the user logs out (exits the login shell), if a file called ~/.bash_logout exists, it is sourced. This file normally contains commands to clean up temporary files, truncate the history file, record the time of logout, etc.
Options to Prevent Startup Files from Being Executed. If bash is invoked with the - -noprofile option (e.g., bash - -noprofile), then the /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile startup files will not be sourced.
If invoked with the -p option (e.g., bash -p), bash will not read the user's ~/.profile file.
If bash is invoked as sh (Bourne shell), it tries to mimic the behavior of the Bourne shell as closely as possible. For a login shell, it attempts to source only /etc/profile and ~/.profile, in that order. The -noprofile option may still be used to disable this behavior. If the shell is invoked as sh, it does not attempt to source any other startup files.
The .inputrc File. Another default initalization file, .inputrc, is also read when bash starts up. This file, if it exists in the user's home directory, contains variables to customize keystroke behavior and settings that bind strings, macros, and control functions to keys. The names for the key bindings and what they do are found in the Readline library, a library that is used by applications that manipulate text. The bindings are used particularly by the built-in emacs and vi editors, when performing command line editing. (See "Command Line Editing" on page 641 for more on readline.)
The set -o Options. The set command can take options when the -o switch is used. Options allow you to customize the shell environment. They are either on or off, and are normally set in the BASH_ENV (ENV) file. Many of the options for the set command are set with an abbreviated form. For example, set -o noclobber can also be written, set -C. (See Table 11.1.)
Name of Option | Shortcut Switch | What It Does |
---|---|---|
allexport | -a | Automatically marks new or modified variables for export from the time the option is set, until unset. |
braceexpand | -B | Enables brace expansion, and is a default setting. |
emacs | For command line editing, uses the emacs built-in editor, and is a default setting. | |
errexit | -e | If a command returns a nonzero exit status (fails), exits. Not set when reading initialization files. |
histexpand | -H | Enables ! and !! when performing history substitution, and is a default setting. |
history | Enables command line history; on by default. | |
ignoreeof | Disables EOF (Control-D) from exiting a shell; must type exit. Same as setting shell variable, IGNOREEOF=10. | |
keyword | -k | Places keyword arguments in the environment for a command. |
interactive-comments | For interactive shells, a leading # is used to comment out any text remaining on the line. | |
monitor | -m | Allows job control. |
noclobber | -C | Protects files from being overwritten when redirection is used. |
noexec | -n | Reads commands, but does not execute them. Used to check the syntax of scripts. Not on when running interactively. |
noglob | -d | Disables pathname expansion; i.e., turns off wildcards. |
notify | -b | Notifies user when background job finishes. |
nounset | -u | Displays an error when expanding a variable that has not been set. |
onecmd | -t | Exit after reading and executing one command. |
physical | -P | If set, does not follow symbolic links when typing cd or pwd. The physical directory is used instead. |
posix | Shell behavior is changed if the default operation doesn't match the POSIX standard. | |
privileged | -p | When set, the shell does not read the .profile or ENV file and shell functions are not inherited from the environment; automatically set for setuid scripts. |
posix | Change the default behavior to POSIX 1003.2. | |
verbose | -v | Turns on the verbose mode for debugging. |
vi | For command line editing, uses the vi built-in editor. | |
xtrace | -x | Turns on the echo mode for debugging. |
FORMATset -o option # Turns on the option. set +o option # Turns off the option. set -[a-z] # Abbreviation for an option; the minus turns it on. set +[a-z] # Abbreviation for an option; the plus turns it off. |
1 set -o allexport 2 set +o allexport 3 set -a 4 set +a
EXPLANATION
|
1 $ set -o braceexpand on errexit off hashall on histexpand on keyword off monitor on noclobber off noexec off noglob off notify off nounset off onecmd off physical off privileged off verbose off xtrace off history on ignoreeof off interactive-comments on posix off emacs off vi on 2 $ set -o noclobber 3 $ date > outfile 4 $ ls > outfile bash: outfile: Cannot clobber existing file. 5 $ set +o noclobber 6 $ ls > outfile 7 $ set -C
EXPLANATION
|
The shopt Built-In (Version 2.x+). The shopt (shell options) built-in command is used in newer versions of bash as an alternative to the set command. In many ways shopt duplicates the set built-in command, but it adds more options for configuring the shell. See Table 11.27 for a list of all the shopt options. In the following example, shopt with the -p option prints all the available options settings. The -u switch indicates an unset option and -s indicates one that is currently set.
1 $ shopt -p shopt -u cdable_vars shopt -u cdspell shopt -u checkhash shopt -u checkwinsize shopt -s cmdhist shopt -u dotglob shopt -u execfail shopt -s expand_aliases shopt -u extglob shopt -u histreedit shopt -u histappend shopt -u histverify shopt -s hostcomplete shopt -u huponexit shopt -s interactive_comments shopt -u lithist shopt -u mailwarn shopt -u nocaseglob shopt -u nullglob shopt -s promptvars shopt -u restricted_shell shopt -u shift_verbose shopt -s sourcepath 2 $ shopt -s cdspell 3 $ shopt -p cdspell shopt -s cdspell 4 $ cd /hame /home 5 $ pwd /home 6 $ cd /ur/lcal/ban /usr/local/man 7 $ shopt -u cdspell 8 $ shopt -p cdspell shopt -u cdspell
EXPLANATION
|
[a] The words switch or option are interchangeable. They are arguments to a command that contain a leading dash.
When used interactively, the shell prompts you for input. When you see the prompt, you know that you can start typing commands. The bash shell provides four prompts: the primary prompt is a dollar sign ($), and the secondary prompt is a right angle bracket symbol (>). The third and fourth prompts, PS3 and PS4 respectively, will be discussed later. The prompts are displayed when the shell is running interactively. You can change these prompts.
The variable PS1 is set to a string containing the primary prompt. Its value, the dollar sign, appears when you log on and waits for user input, normally a UNIX command. The variable PS2 is the secondary prompt, initially set to the right angle bracket character (>). It appears if you have partially typed a command and then pressed Enter. You can change the primary and secondary prompts.
The Primary Prompt. The dollar sign (or bash $) is the default primary prompt. You can change your prompt. Normally prompts are defined in /etc/bashrc or the user initialization file, .bash_profile, or .profile (Bourne shell).
1 $ PS1="$(uname -n) > " 2 chargers >
EXPLANATION
|
[a] The command, uname -n, is executed because it is enclosed in a set of parentheses preceded by a dollar sign. An alternative would be to enclose the command in backquotes. See "Command Substitution".)
Setting the Prompt with Special Escape Sequences. By inserting special backslash/escape sequences into the prompt string, you can customize the prompts. Table 11.2 lists the special sequences.
Backslash Sequence | What It Evaluates To |
---|---|
\t | The current time in HH:MM:SS format. |
\d | The date in "Weekday Month Date" format (e.g., Tue May 26). |
\n | Newline. |
\s | The name of the shell, the basename of $0 (the portion following the final slash). |
\w | The current working directory. |
\W | The basename of the current working directory. |
\u | The username of the current user. |
\h | The hostname. |
\# | The command number of this command. |
\! | The history number of this command. |
\$ | If the effective UID is 0, a #, otherwise a $. |
\nnn | The character corresponding to the octal number nnn. |
\\ | Backslash. |
\[ | Begin a sequence of nonprinting characters; could be used to embed a terminal control sequence into the prompt. |
\] | End a sequence of nonprinting characters. |
New in bash Version 2.x+: | |
\a | The ASCII bell character. |
\@ | The current time in 12-hour AM/PM format. |
\H | The hostname. |
\T | The current time in 12-hour format: HH:MM:SS. |
\e | The ASCII escape character (033). |
\v | The version of bash, e.g., 2.03. |
\V | The release and pathlevel of bash; e.g., 2.03.0. |
1 $ PS1="[\u@\h \W]\\$ " [ellie@homebound ellie]$ 2 $ PS1="\W:\d> " ellie:Tue May 18>
EXPLANATION
|
The Secondary Prompt. The PS2 variable is assigned a string called the secondary prompt. Its value is displayed to standard error, which is the screen by default. This prompt appears when you have not completed a command or more input is expected. The default secondary prompt is >.
1 $ echo "Hello 2 > there" 3 Hello there 4 $ 5 $ PS2="----> " 6 $ echo 'Hi 7 ------> ------> ------>there' Hi there $ 8 $ PS2="\s:PS2 > " $ echo 'Hello bash:PS2 > what are bash:PS2 > you bash:PS2 > trying to do? bash:PS2 > ' Hello what are you trying to do? $
EXPLANATION
|
The Search Path. Bash uses the PATH variable to locate commands typed at the command line. The path is a colon-separated list of directories used by the shell when searching for commands. The default path is system-dependent, and is set by the administrator who installs bash. The path is searched from left to right. The dot at the end of the path represents the current working directory. If the command is not found in any of the directories listed in the path, the shell sends to standard error the message filename: not found. The path is normally set in the .bash_profile if running the bash shell or .profile file if using sh, the Bourne shell.
If the dot is not included in the path and you are executing a command or script from the current working directory, the name of the script must be preceded with a ./, such as ./program_name, so that shell can find the program.
(Printing the PATH) 1 $ echo $PATH /usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:. (Setting the PATH) 2 $ PATH=$HOME:/usr/ucb:/usr:/usr/bin:/usr/local/bin: 3 $ export PATH 4 $ runit bash: runit: command not found 5 $ ./runit < program starts running here >
EXPLANATION
|
The hash Command. The hash command controls the internal hash table used by the shell to improve efficiency in searching for commands. Instead of searching the path each time a command is entered, the first time you type a command, the shell uses the search path to find the command, and then stores it in a table in the shell's memory. The next time you use the same command, the shell uses the hash table to find it. This makes it much faster to access a command than having to search the complete path. If you know that you will be using a command often, you can add the command to the hash table. You can also remove commands from the table. The output of the hash command displays the number of times the shell has used the table to find a command (hits) and the full pathname of the command. The hash command with the -r option clears the hash table. An argument of - - disables option checking for the rest of the arguments. Hashing is automatically implemented by bash. Although you can turn it off, if there isn't any compelling reason to do so, don't.
(Printing the PATH) (Command line) 1 hash hits command 1 /usr/bin/mesg 4 /usr/bin/man 2 /bin/ls 2 hash -r 3 hash No commands in hash table 4 hash find hits command 0 /usr/bin/find
EXPLANATION
|
The source or dot Command. The source command (from the C shell) is a built-in bash shell command. The dot command, simply a period, (from the Bourne shell) is another name for source. Both commands take a scriptname as an argument. The script will be executed in the environment of the current shell; that is, a child process will not be started. All variables set in the script will become part of the current shell's environment. Likewise, all variables set in the current shell will become part of the script's environment. The source (or dot) command is normally used to reexecute any of the initialization files, e.g., .bash_profile, .profile, etc, if they have been modified. For example, if one of the settings, such as the EDITOR or TERM variable, has been changed in the .bash_profile since you logged on, you can use the source command to reexecute commands in the .bash_profile without logging out and then logging back on. A file, such as .bash_profile, or for that matter any shell script, does not need execute permissions to be sourced with either the dot or the source commands.
$ source .bash_profile $ . .bash_profile
EXPLANATIONThe source or dot command executes the initialization file, .bash_profile, within the context of the current shell. Local and global variables are redefined within this shell. The dot command makes it unnecessary to log out and then log back in again after the file has been modified.[a] |
[a] If the .bash_profile were executed directly as a script, a child shell would be started. Then the variables would be set in the child shell, and when the child shell exited, the parent shell would not have any of the settings available to it.
After you log on, the bash shell displays its primary prompt, a dollar sign by default. The shell is your command interpreter. When the shell is running interactively, it reads commands from the terminal and breaks the command line into words. A command line consists of one or more words (or tokens), separated by whitespace (blanks and/or tabs), and terminated with a newline, generated by pressing the Enter key. The first word is the command, and subsequent words are the command's arguments. The command may be a UNIX executable program such as ls or date, a user-defined function, a built-in command such as cd or pwd, or a shell script. The command may contain special characters, called metacharacters, which the shell must interpret while parsing the command line. If a command line is too long, the backslash character, followed by a newline, will allow you to continue typing on the next line. The secondary prompt will appear until the command line is terminated.
The Order of Processing Commands. The first word on the command line is the command to be executed. The command may be a keyword, an alias, a function, a special built-in command or utility, an executable program, or a shell script. The command is executed according to its type in the following order:
Aliases
Keywords (such as if, function, while, until)
Functions
Built-in commands
Executables and scripts
Special built-in commands and functions are defined within the shell, and therefore are executed from within the context of the current shell, making them much faster in execution. Scripts and executable programs such as ls and date are stored on disk, and the shell, in order to execute them, must first locate them within the directory hierarchy by searching the PATH environment variable; the shell then forks a new shell that executes the script. To find out the type of command you are using i.e., a built-in command, an alias, a function, or an executable, etc. use the built-in type command. (See Example 11.17.)
$ type pwd pwd is a shell builtin $ type test test is a shell builtin $ type clear clear is /usr/bin/clear $ type m m is aliased to 'more' $ type bc bc is /usr/bin/bc $ type if if is a shell keyword $ type -path cal /usr/bin/cal $ type which which is aliased to 'type -path' $ type greetings greetings is a function greetings () { echo "Welcome to my world!"; }
Built-In Commands and the help Command. Built-in commands are commands that are part of the internal source code for the shell. They are built-in and readily available to the shell, whereas commands such as date, cal, and finger are compiled binary programs that reside on the disk. There is less overhead in executing a built-in because it involves no disk operations. Built-in commands are executed by the shell before the programs on disk. Bash has added a new online help system so that you can see all the built-ins, or a description for a particular built-in; help itself is a built-in command. See Table 11.28 for a complete list of built-in commands.
1 $ help help help: help [pattern ...] Display helpful information about built-in commands. if PATTERN is specified, gives detailed help on all commands matching PATTERN, otherwise a list of the built-ins is printed. 2 $ help pw pwd: pwd Print the current working directory.
Changing the Order of Command Line Processing. Bash provides three built-in commands that can override the order of command line processing: command, builtin, and enable.
The command built-in eliminates aliases and functions from being looked up in the order of processing. Only built-ins and executables, found in the search path, will be processed.
The builtin command looks up only built-ins, ignoring functions and executables found in the path.
The enable built-in command turns built-ins on and off. By default, built-ins are enabled. Disabling a built-in allows an executable command found on the disk to be to be executed without specifying a full pathname, even if it has the same name as a built-in. (In normal processing, bash searches for built-ins before disk executable commands.) Built-ins become disabled by using the -n switch. For example, a classic confusion for new shell programmers is naming a script test. Because test is a built-in command, the shell will try to execute it rather than the user's script (a built-in is normally executed before any executable program). By typing: enable -n test, the test built-in is disabled, and the user's script will take precedence.
Without options, the enable built-in prints a list of all the built-ins. Each of the following built-ins are described in "Shell Built-In Commands".
1 $ enable enable . enable : enable [ enable alias enable bg enable bind enable break enable builtin enable cd enable command enable continue enable declare enable dirs ..... enable read enable readonly enable return enable set enable shift enable shopt ..... enable type enable typeset enable ulimit enable umask enable unalias enable unset enable wait 2 enable -n test 3 function cd { builtin cd; echo $PWD; }
EXPLANATION
|
The Exit Status. After a command or program terminates, it returns an exit status to the parent process. The exit status is a number between 0 and 255. By convention, when a program exits, if the status returned is zero, the command was successful in its execution. When the exit status is nonzero, the command failed in some way. If a command is not found by the shell, the exit status returned is 127. If a fatal signal causes the command to terminate, the exit status is 128 plus the value of the signal that caused it to die.
The shell status variable, ?, is set to the value of the exit status of the last command that was executed. Success or failure of a program is determined by the programmer who wrote the program.
1 $ grep ellie /etc/passwd ellie:MrHJEFd2YpkJY:501:501::/home/ellie:/bin/bash 2 $ echo $? 0 3 $ grep nicky /etc/passwd 4 $ echo $? 1 5 $ grep ellie /junk grep: /junk: No such file or directory 6 $ echo $? 2 7 $ grip ellie /etc/passwd bash: grip: command not found 8 $ echo $? 127 9 $ find / -name core ^C User presses Control-C 10 $ echo $? 130
EXPLANATION
|
Multiple Commands at the Command Line. A command line can consist of multiple commands. Each command is separated by a semicolon, and the command line is terminated with a newline. The exit status is that of the last command in the chain of commands.
$ ls; pwd; date
EXPLANATIONThe commands are executed from left to right, one after the other, until the newline is reached. |
Grouping Commands. Commands may also be grouped so that all of the output is either piped to another command or redirected to a file.
$ ( ls; pwd; date ) > outputfile
EXPLANATIONThe output of each of the commands is sent to the file called outputfile. The spaces inside the parentheses are necessary. |
Conditional Execution of Commands. With conditional execution, two command strings are separated by the special metacharacters, double ampersands (&&) and double vertical bars (||). The command on the right of either of these metacharacters will or will not be executed based on the exit condition of the command on the left.
$ cc prgm1.c -o prgm1 && prgm1
EXPLANATIONIf the first command is successful (has a zero exit status), the command after the && is executed; i.e., if the cc program can successfully compile prgm1.c, the resulting executable program, prgm1, will be executed. |
$ cc prog.c >& err || mail bob < err
EXPLANATIONIf the first command fails (has a nonzero exit status), the command after the || is executed; i.e., if the cc program cannot compile prog.c, the errors are sent to a file called err, and user bob will be mailed the err file. |
Commands in the Background. Normally, when you execute a command, it runs in the foreground, and the prompt does not reappear until the command has completed execution. It is not always convenient to wait for the command to complete. When you place an ampersand (&) at the end of the command line, the shell will return the shell prompt immediately and execute the command in the background concurrently. You do not have to wait to start up another command. The output from a background job will be sent to the screen as it processes. Therefore, if you intend to run a command in the background, the output of that command might be redirected to a file or piped to another device, such as a printer, so that the output does not interfere with what you are doing.
The ! variable contains the PID number of the last job put in the background. (See "Job Control" for more on background processing.)
1 $ man sh | lp& 2 [1] 1557 3 $ kill -9 $!
EXPLANATION
|
Job control is a powerful feature of the bash shell that allows you to selectivly run programs, called jobs, in the background or foreground. A running program is called a process or a job and each process has a process ID number, called the PID. Normally, a command typed at the command line is running in the foreground and will continue until it has finished unless you send a signal by pressing Ctrl-C or Ctrl-\ to terminate it. With job control you can send a job to the background and let it keep running; you can stop a job by pressing Ctrl-Z, which sends the job to the background and suspends it; you can cause a stopped job to run in the background; you can bring a background job back to the foreground; and you can even kill the jobs you have running in the background or foreground. For a list of job commands, see Table 11.3.
By default, job control is already set (some older versions of UNIX do not support this feature). If disabled, it can be reset by any one of the following commands:
FORMATset -m # set job control in the .bashrc file set -o monitor # set job control in the .bashrc file bash -m -i # set job control when invoking interactive bash |
1 $ vi [1]+ Stopped vi 2 $ sleep 25& [2] 4538 3 $ jobs [2]+ Running sleep 25& [1]- Stopped vi 4 $ jobs -l [2]+ 4538 Running sleep 25& [1]- 4537 Stopped vi 5 $ jobs %% [2]+ 4538 Running sleep 25& 6 $ fg %1 7 $ jobs -x echo %1 4537 8 $ kill %1 # or kill 4537 [1]+ Stopped vi Vim: Caught deadly signal TERM Vim: Finished. [1]+ Exit 1 vi
EXPLANATION
|
Command | Meaning |
---|---|
jobs | Lists all the jobs running. |
^Z (Ctrl-Z) | Stops (suspends) the job; the prompt appears on the screen. |
bg | Starts running the stopped job in the background. |
fg | Brings a background job to the foreground. |
stop | Suspends a background job. |
stty tostop | Suspends a background job if it sends output to the terminal. |
kill | Sends the kill signal to a specified job. |
wait [n] | Waits for a specified job and returns its exit status, where n is a PID or job number. |
Argument to jobs command | Represents |
---|---|
%n | Job number n. |
%sting | Job name starting with string. |
%?string | Job name containing string. |
%% | Current job. |
%+ | Current job. |
%- | Previous job, before current job. |
-r | Lists all running jobs. |
-s | Lists all suspended jobs. |
New jobs Options. Two new options were added to the jobs command in bash versions 2.x. They are the -r and -s options. The -r option lists all running jobs, and the -s option lists all stopped jobs.
The disown Built-In. The disown built-in command (bash 2.x) removes a specified job from the job table. After the job has been removed, the shell will no longer recognize it as a viable job process and it can only be referenced by its process ID number.
To save typing, bash implements command and filename completion, a mechanism that allows you to type part of a command or filename, press the Tab key, and the rest of the word will be completed for you.
If you type the first letters in a command and press the Tab key, bash will attempt to complete the command and execute it. If bash cannot complete the filename or command, because neither exists, the terminal may beep and the cursor will stay at the end of the command. If there is more than one command starting with those characters and you press the Tab key a second time, all commands that start with those characters will be listed.
If there are several files starting with the same letters, bash will complete the shortest name that matches, expand out the filename until the characters differ, and then flash the cursor for you to complete the rest.
1 $ ls file1 file2 foo foobarckle fumble 2 $ ls fu[tab] # Expands to filename to fumble 3 $ ls fx[tab] # Terminal beeps, nothing happens 4 $ ls fi[tab] # Expands to file_ (_ is a cursor) 5 $ ls fi[tab][tab] # Lists all possibilities file1 file2 6 $ ls foob[tab] # Expands to foobarckle 7 $ da[tab] # Completes the date command date Tue Feb 28 18:53:40 PST 2001 8 $ ca[tab][tab] # Lists all commands starting with ca cal captoinfo case cat
EXPLANATION
|
The history mechanism keeps a history list, a numbered record of the commands that you have typed at the command line. During a login session, the commands you type are stored in the the shell's memory in a history list and then appended to the history file when you exit. You can recall a command from the history list and reexecute it without retyping the command. The history built-in command displays the history list. The default name for the history file is .bash_history, and it is located in your home directory.
When bash starts accessing the history file, the HISTSIZE variable specifies how many commands can be copied from the history file into the history list. The default size is 500. The HISTFILE variable specifies the name of the command history file (~/.bash_history is the default) where commands are stored. If unset, the command history is not saved when an interactive shell exits.
The history file grows from one login session to the next. The HISTFILESIZE variable controls the maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated when it surpasses that number of lines. The default size is 500.
The fc -l command can be used to display or edit commands in the history list.
FCEDIT | The pathname of the UNIX editor that uses the fc command. |
HISTCMD | The history number, or index in the history list, of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset. |
HISTCONTROL | If set to a value of ignorespace, lines that begin with a space character are not entered on the history list. If set to a value of ignoredups, lines matching the last history line are not entered. A value of ignoreboth combines the two options. If unset, or if set to any other value than those above, all lines read by the parser are saved on the history list. |
HISTFILE | Specifies file in which to store command history. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits. |
HISTFILESIZE | The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines. The default value is 500. |
HISTIGNORE | A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored to the beginning of the line and consists of normal shell pattern matching characters. An & can be used in the pattern causing the history command to ignore duplicates; e.g., ty??:& would match for any command line starting with ty followed by two characters, and any duplicates of that command. Those commands would not be placed in the history list. |
HISTSIZE | The number of commands to remember in the command history. The default value is 500. |
The Arrow Keys. To access commands from the history file, you can use the arrow keys on the keyboard to move up and down through the history file, and from left to right. You can edit any of the lines from the history file by using the standard keys for deleting, updating, backspacing, etc. As soon as you have edited the line, pressing the Enter key will cause the command line to be reexecuted.
Up arrow moves up the history list. | |
Down arrow moves down the history list. | |
Right arrow moves cursor to right on history command. | |
Left arrow moves left on history command. |
The history Built-In Command. The history built-in command displays the history of commands typed preceded by an event number.
1 $ history 982 ls 983 for i in 1 2 3 984 do 985 echo $i 986 done 987 echo $i 988 man xterm 989 adfasdfasdfadfasdfasdfadfasdfasdf 990 id -gn 991 id -un 992 id -u 993 man id 994 more /etc/passwd 995 man ulimit 996 man bash 997 man baswh 998 man bash 999 history 1000 history
EXPLANATION
|
The fc Command. The fc command, also called the fix command, can be used in two ways: (1) to select commands from the history list, and (2) to edit the commands in either the vi or emacs editor, or for that matter, any editor on your system.
In the first form, fc with the -l option can select specific lines or ranges of lines from the history list. When the -l switch is on, the output goes to the screen. For example, fc -l, the default, prints the last 16 lines from the history list, fc -l 10 selects lines numbered 10 through the end of the list, and fc -l -3 selects the last three lines. The -n switch turns off the numbering of commands in the history list. With this option on, you could select a range of commands and redirect them to a file, which in turn could be executed as a shell script. The -r switch reverses the order of the commands.
The second form of fc is described in "Command Line Editing" on page 641.
fc Argument | Meaning |
---|---|
-e editor | Puts history list into editor. |
-l n-m | Lists commands in range from n to m. |
-n | Turns off numbering of history list. |
-r | Reverses the order of the history list. |
-s string | Accesses command starting with string. |
1 $ fc -l 4 ls 5 history 6 exit 7 history 8 ls 9 pwd 10 clear 11 cal 2000 12 history 13 vi file 14 history 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history 2 $ fc -l -3 19 cd 20 history 21 fc -l 3 $ fc -ln exit history ls pwd clear cal 2000 history vi file history ls -l date more file echo a b c d cd history fc -l fc -l -3 4 $ fc -ln -3 > saved 5 $ more saved fc -l fc -l -3 fc -ln 6 $ fc -l 15 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history 21 fc -l 22 fc -l -3 23 fc -ln 24 fc -ln -3 > saved 25 more saved 26 history 7 $ fc -l 15 20 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history
EXPLANATION
|
If fc is given the -s option, a string pattern can be used to reexecute a previous command; e.g., fc -s rm will cause the most previous line containing the pattern rm to be reexecuted. To emulate the Korn shell's redo command, you can create a bash alias called r, e.g., alias r='fc -s' so that if you type r vi at the command line, the last history item containing that pattern will be reexecuted; in this case, the vi editor will be started just as it was the last time it started, including any arguments passed.
1 $ history 1 ls 2 pwd 3 clear 4 cal 2000 5 history 6 ls -l 7 date 8 more file 9 echo a b c d 2 $ fc -s da date Thu Jul 15 12:33:25 PST 2001 3 $ alias r="fc -s" 4 $ date +%T 18:12:32 5 $ r d date +%T 18:13:19
EXPLANATION
|
Reexecuting History Commands (bang! bang!). To reexecute a command from the history list, the exclamation point (called bang) is used. If you type two exclamation points (!!) bang, bang, the last command in the history list is reexecuted. If you type an exclamation point, followed by a number, the command listed by that number is reexecuted. If you type an exclamation point and a letter or string, the last command that started with that letter or string is reexecuted. The caret (^) is also used as a shortcut method for editing the previous command. See Table 11.7 for a complete list of history substitution characters.
Event Designator | Meaning |
---|---|
! | Indicates the start of history substitution. |
!! | Reexecutes the previous command. |
!N | Reexecutes the Nth command from the history list. |
!-N | Reexecutes the Nth command back from present command. |
!string | Reexecutes the last command starting with string. |
!?string? | Reexecutes the last command containing string. |
!?string?% | Reexecutes the most recent command line argument from the history list containing string. |
!$ | Uses the last argument from the last history command in the current command line. |
!! string | Appends string to the previous command and executes. |
!N string | Appends string to Nth command in history list and executes. |
!N:s/old/new/ | In previous Nth command, substitutes the first occurrence of old string with new string. |
!N:gs/old/new/ | In previous Nth command, globally substitutes old string with new string. |
^old^new^ | In last history command, substitutes old string with new string. |
command !N:wn | Executes current command appending an argument (wn) from the Nth previous command. Wn is a number starting at 0, 1, 2, designating the number of the word from the previous command; word 0 is the command itself, and 1 is its first argument, etc. (See Example 11.32.) |
1 $ date Mon Jul 12 12:27:35 PST 2001 2 $ !! date Mon Jul 12 12:28:25 PST 2001 3 $ !106 date Mon Jul 12 12:29:26 PST 2001 4 $ !d date Mon Jul 12 12:30:09 PST 2001 5 $ dare dare: Command not found. 6 $ ^r^t date Mon Jul 12 12:33:25 PST 2001
EXPLANATION
|
1 $ ls file1 file2 file3 file1 file2 file3 $ vi !:1 vi file1 2 $ ls file1 file2 file file1 file2 file3 $ ls !:2 ls file2 file2 3 $ ls file1 file2 file3 $ ls !:3 ls file3 file3 4 $ echo a b c a b c $ echo !$ echo c c 5 $ echo a b c a b c $ echo !^ echo a a 6 % echo a b c a b c % echo !* echo a b c a b c 7 % !!:p echo a b c
EXPLANATION
|
Command Line Editing. The bash shell provides two built-in editors, emacs and vi, that allow you to interactively edit your history list. When you use the editing features at the command line, whether in vi or emacs mode, the readline functions determine which keys will perform certain functions. For example, if using emacs, Ctrl-P allows you to scroll upward in the command line history, whereas if using vi, the K key moves upward through the history list. Readline also controls the arrow keys, cursor movement, changing, deleting, inserting text, and redoing or undoing corrections. Another feature of readline is the completion feature previously discussed in "Command and Filename Completion". This allows you to type part of a command, filename, or variable, and then, by pressing the Tab key, the rest of the word is completed. There are many more features provided by the Readline library designed to help manipulate text at the command line.
The emacs built-in editor is the default built-in editor and is modeless, whereas the vi built-in editor works in two modes, one to execute commands on lines and the other to enter text. If you use UNIX, you are probably familiar with at least one of these editors. To enable the vi editor, add the set command listed below[5] and put this line in your ~/.bashrc file. To set vi, type what's shown in the following example, at either the prompt or in the ~/.bashrc file.
set -o vi
EXPLANATIONSets the built-in vi editor for command line editing of the history list. |
To switch to the emacs editor, type:
set -o emacs
EXPLANATIONSets the built-in emacs editor for command line editing of the history list. |
The vi Built-In Editor. To edit the history list, go to the command line and press the Esc key. Then press the K key if you want to scroll upward in the history list, and the J key[6] to move downward, just like standard vi motion keys. When you find the command that you want to edit, use the standard keys that you would use in vi for moving left and right, deleting, inserting, and changing text. (See Table 11.8.) After making the edit, press the Enter key. The command will be executed and added to the bottom of the history list.
Command | Function |
---|---|
Moving Through the History File: | |
Esc k or + | Move up the history list. |
Esc j or - | Move down the history list. |
G | Move to first line in history file. |
5G | Move to fifth command in history file for string. |
/string | Search upward through history file. |
? | String search downward through history file. |
Moving Around on a Line: | |
h | Move left on a line. |
l | Move right on a line. |
b | Move backward a word. |
e or w | Move forward a word. |
^ or 0 | Move to beginning of first character on the line. |
$ | Move to end of line. |
Editing with vi: | |
a A | Append text. |
i I | Insert text. |
dd dw x | Delete text into a buffer (line, word, or character). |
cc C | Change text. |
u U | Undo. |
yy Y | Yank (copy a line into buffer). |
p P | Put yanked or deleted line down below or above the line. |
r R | Replace a letter or any amount of text on a line. |
The emacs Built-In Editor. If using the emacs built-in editor, like vi, start at the command line. To start moving upward through the history file, press ^P. To move down, press ^N. Use emacs editing commands to change or correct text, then press Enter and the command will be reexecuted. See Table 11.9.
Command | Function |
---|---|
Ctrl-P | Move up history file. |
Ctrl-N | Move down history file. |
Ctrl-B | Move backward one character. |
Ctrl-R | Search backward for string. |
Esc B | Move backward one word. |
Ctrl-F | Move forward one character. |
Esc F | Move forward one word. |
Ctrl-A | Move to the beginning of the line. |
Ctrl-E | Move to the end of the line. |
Esc < | Move to the first line of the history file. |
Esc > | Move to the last line of the history file. |
Editing with emacs: | |
Ctrl-U | Delete the line. |
Ctrl-Y | Put the line back. |
Ctrl-K | Delete from cursor to the end line. |
Ctrl-D | Delete a letter. |
Esc D | Delete one word forward. |
Esc H | Delete one word backward. |
Esc space | Set a mark at cursor position. |
Ctrl-X Ctrl-X | Exchange cursor and mark. |
Ctrl-P Ctrl-Y | Push region from cursor to mark into a buffer (Ctrl-P) and put it down (Ctrl-Y). |
FCEDIT and Editing Commands. If the fc command is given the -e option followed by the name of a UNIX editor, that editor is invoked containing history commands selected from the history list; e.g., fc -e vi -1 -3 will invoke the vi editor, create a temporary file in /tmp, with the last three commands from the history list in the vi buffer. The commands can be edited or commented out. (Preceding the command with a # will comment it.) If the user quits the editor, the commands will all be echoed and executed.[7]
If the editor name is not given, the value of the FCEDIT variable is used (typically set in the initialization files, either bash_profile or .profile), and the value of the EDITOR variable is used if FCEDIT is not set. When editing is complete, and you exit the editor, all of the edited commands are echoed and executed.
EXPLANATION
|
An alias is a bash user-defined abbreviation for a command. Aliases are useful if a command has a number of options and arguments or the syntax is difficult to remember. Aliases set at the command line are not inherited by subshells. Aliases are normally set in the .bashrc file. Because the .bashrc is executed when a new shell is started, any aliases set there will be reset for the new shell. Aliases may also be passed into shell scripts but will cause potential portability problems unless they are set directly within the script.
Listing Aliases. The alias built-in command lists all set aliases. The alias is printed first, followed by the real command or commands it represents.
$ alias alias co='compress' alias cp='cp -i' alias mroe='more' alias mv='mv -i' alias ls='ls --colorztty' alias uc='uncompress'
EXPLANATIONThe alias command lists the alias (nickname) for the command and the real command the alias represents after the = sign. |
Creating Aliases. The alias command is used to create an alias. The first argument is the name of the alias, the nickname for the command. The rest of the line consists of the command or commands that will be executed when the alias is executed. Bash aliases cannot take arguments (see "Defining Functions" on page 693). Multiple commands are separated by a semicolon, and commands containing spaces and metacharacters are surrounded by single quotes.
1 $ alias m=more 2 $ alias mroe=more 3 $ alias lF='ls -alF' 4 $ alias r='fc -s'
EXPLANATION
|
Deleting Aliases. The unalias command is used to delete an alias. To temporarily turn off an alias, precede the alias name by a backslash.
1 $ unalias mroe 2 $ \ls
EXPLANATION
|
If you find that as you work, you cd up and down the directory tree into many of the same directories, you can make it easy to access those directories by pushing them onto a directory stack and manipulating the stack. The pushd built-in command pushes directories onto a stack and the popd command removes them. (See Example 11.39.) The stack is a list of directories with the directory at the left being the most recent directory pushed onto the stack. The directories can be listed with the built-in dirs command.
The dirs Built-In Command. The built-in command dirs, with a -l option, displays the directory stack with each of its directories in full pathname format; without an option, dirs uses a tilde to denote the home directory. With a +n option, dirs displays the nth directory entry counting from the left in the directory list, starting at 0. With -n option, it does the same thing, but starts at the right-hand side of the directory list with 0.
The pushd and popd Commands. The pushd command, with a directory as an argument, causes the new directory to be added to the directory stack and, at the same time, changes to that directory. If the argument is a +n where n is a number, pushd rotates the stack so that the nth directory from the stack, starting at the left-hand side, is pushed onto the top of the stack. With a -n option, it does the same thing but starts at the right-hand side. Without arguments, pushd exchanges the top two elements of the directory stack, making it easy to switch back and forth between directories.
The popd command removes a directory from the top of the stack, and changes to that directory. With +n, where n is a number, popd removes the nth entry, starting at the left of the list shown by dirs.
1 $ pwd /home/ellie $ pushd .. /home ~ $ pwd /home 2 $ pushd # Swap the two top directories on the stack ~ /home $ pwd /home/ellie 3 $ pushd perlclass ~/perlclass ~ /home 4 $ dirs ~/perlclass ~ /home 5 $ dirs -l /home/ellie/perlclass /home/ellie /home 6 $ popd ~/home $ pwd /home/ellie 7 $ popd /home $ pwd /home 8 $ popd bash: popd: Directory stack empty.
EXPLANATION
|
Metacharacters are special characters used to represent something other than themselves. Shell metacharacters are called wildcards. Table 11.10 lists metacharacters and what they do.
Metacharacter | Meaning |
---|---|
\ | Literally interprets the following character. |
& | Processes in the background. |
; | Separates commands. |
$ | Substitutes variables. |
? | Matches for a single character. |
[abc] | Matches for one character from a set of characters; e.g., a, b, or c. |
[!abc] | Matches for one character not from the set of characters; e.g., not a, b, or c. |
* | Matches for zero or more characters. |
(cmds) | Executes commands in a subshell. |
{cmds} | Executes commands in current shell. |
When evaluating the command line, the shell uses metacharacters to abbreviate filenames or pathnames that match a certain set of characters. The filename substitution metacharacters listed in Table 11.11 are expanded into an alphabetically listed set of filenames. The process of expanding the metacharacter into filenames is also called filename substitution, or globbing. If a metacharacter is used and there is no filename that matches it, the shell treats the metacharacter as a literal character.
Metacharacter | Meaning |
---|---|
* | Matches zero or more characters. |
? | Matches exactly one character. |
[abc] | Matches one character in the set a, b, or c. |
[!abc] | Matches one character not in the set, not a, b, or c. |
{a,ile,ax} | Matches for a character or set of characters. |
[! a z] | Matches one character not in the range from a to z. |
\ | Escapes or disables the metacharacter. |
The Asterisk. The asterisk is a wildcard that matches for zero or more of any characters in a filename.
1 $ ls * abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak none nonsense nobody nothing nowhere one 2 $ ls *.bak file1.bak file2.bak 3 $ echo a* ab abc1 abc122 abc123 abc2
EXPLANATION
|
The Question Mark. The question mark represents a single character in a filename. When a filename contains one or more question marks, the shell performs filename substitution by replacing the question mark with the character it matches in the filename.
1 $ ls abc abc122 abc2 file1.bak file2.bak nonsense nothing one abc1 abc123 file1 file2 none noone nowhere 2 $ ls a?c? abc1 abc2 3 $ ls ?? ls: ??: No such file or directory 4 $ echo abc??? abc122 abc123 5 $ echo ?? ??
EXPLANATION
|
The Square Brackets. The brackets are used to match filenames containing one character in a set or range of characters.
1 $ ls abc abc122 abc2 file1.bak file2.bak nonsense nothing one abc1 abc123 file1 file2 none noone nowhere 2 $ ls abc[123] abc1 abc2 3 $ ls abc[1 3] abc1 abc2 4 $ ls [a z][a z][a z] abc one 5 $ ls [!f z]??? abc1 abc2 6 $ ls abc12[23] abc122 abc123
EXPLANATION
|
Brace Expansion. The curly braces match for any of a list of comma-separated strings. Normally the strings are filenames. Any characters prepended to the opening curly brace are called the preamble, and any characters appended to the closing curly brace are called the postamble. Both the preamble and postamble are optional. There can be no unquoted whitespace within the braces.
1 $ ls a.c b.c abc ab3 ab4 ab5 file1 file2 file3 file4 file5 foo faa fumble 2 $ ls f{oo,aa,umble} foo faa fumble 3 $ ls a{.c,c,b[3-5]} a.c ab3 ab4 ab5 4 $ mkdir /usr/local/src/bash/{old,new,dist,bugs} 5 $ chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} 6 $ echo fo{o, um}* fo{o, um}* 7 $ echo {mam,pap,ba}a mama papa baa 8 $ echo post{script,office,ure} postscript postoffice posture
EXPLANATION
|
Escaping Metacharacters. To use a metacharacter as a literal character, use the backslash to prevent the metacharacter from being interpreted.
1 $ ls abc file1 youx 2 $ echo How are you? How are youx 3 $ echo How are you\? How are you? 4 $ echo When does this line \ > ever end\? When does this line ever end?
EXPLANATION
|
Tilde and Hyphen Expansion. The tilde character was adopted by the bash shell (from the C shell) for pathname expansion. The tilde by itself evaluates to the full pathname of the user's home directory.[8] When the tilde is appended with a username, it expands to the full pathname of that user.
When the plus sign follows the tilde, the value of the PWD (present working directory) replaces the tilde. The tilde followed by the hyphen character is replaced with the previous working directory; OLDPWD also refers to the previous working directory.
1 $ echo ~ /home/jody/ellie 2 $ echo ~joe /home/joe 3 $ echo ~+ /home/jody/ellie/perl 4 $ echo ~- /home/jody/ellie/prac 5 $ echo $OLDPWD /home/jody/ellie/prac 6 $ cd - /home/jody/ellie/prac
EXPLANATION
|
Controlling Wildcards (Globbing). If the bash noglob variable is set or if the set command is given a -f option, filename substitution, called globbing, is turned off, meaning that all metacharacters represent themselves; they are not used as wildcards. This can be useful when searching for patterns containing metacharacters in programs like grep, sed, or awk. If globbing is not set, all metacharacters must be escaped with a backslash to turn off wildcard interpretation.
The built-in shopt command (bash versions 2.x) also supports options for controlling globbing.
1 $ set noglob or set -f 2 $ print * ?? [] ~ $LOGNAME * ?? [] /home/jody/ellie ellie 3 $ unset noglob or set +f 4 $ shopt -s dotglob # Only available in bash versions 2.x 5 $ echo *bash* .bash_history .bash_logout .bash_profile .bashrc bashnote bashtest
EXPLANATION
|
Extended Filename Globbing (bash 2.x). Derived from Korn shell pattern matching, bash 2.x has included this extended functionality, allowing regular expression-type syntax. The regular expression operators are not recognized unless the extglob option to the shopt command is turned on:
shopt -s extglob
Regular Expression | Meaning |
---|---|
abc?(2|9)1 | ? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an or condition; e.g., either 2 or 9. Matches abc21, abc91, or abc1. |
abc*([0 9]) | * matches zero or more occurrences of any pattern in the parentheses. Matches abc followed by zero or more digits; e.g., abc, abc1234, abc3, abc2, etc. |
abc+([0 9]) | + matches one or more occurrences of any pattern in the parentheses. Matches abc followed by one or more digits; e.g., abc3, abc123, etc. |
no@(one|ne) | @ matches exactly one occurrence of any pattern in the parentheses. Matches noone or none. |
no!(thing|where) | ! matches all strings except those matched by any of the patterns in the parentheses. Matches no, nobody, or noone, but not nothing or nowhere. |
1 $ shopt -s extglob 2 $ ls abc abc122 f1 f3 nonsense nothing one abc1 abc2 f2 none noone nowhere 3 $ ls abc?(1|2) abc abc1 abc2 4 $ ls abc*([1-5]) abc abc1 abc122 abc2 5 $ ls abc+([0-5]) abc1 abc122 abc2 6 $ ls no@(thing|ne) none nothing 7 $ ls no!(thing) none nonsense noone nowhere
EXPLANATION
|
Types of Variables. There are two types of variables: local and environment. Local variables are known only to the shell in which they were created. Environment variables are available to any child processes spawned from the shell from which they were created. Some variables are created by the user and others are special shell variables.
Naming Conventions. Variable names must begin with an alphabetic or underscore character. The remaining characters can be alphabetic, decimal digits (0 to 9), or an underscore character. Any other characters mark the termination of the variable name. Names are case-sensitive. When assigning a value to a variable, do not include any whitespace surrounding the equal sign. To set the variable to null, follow the equal sign with a newline. The simplest format for creating a local variable is to assign a value to a variable in the following format.
FORMATvariable=value |
name=Tommy
The declare Built-In. There are two built-in commands, declare and typeset, used to create variables, with options to control the way the variable is set. The typeset command (from Korn shell) is exactly the same as the declare command (bash). The bash documentation says, "The typeset command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the declare built-in command."[9] So from this point on we'll use the declare built-in (even though we could just as easily have chosen to use typeset).
Without arguments, declare lists all set variables. Normally read-only variables cannot be reassigned or unset. If read-only variables are created with declare, they cannot be unset, but they can be reassigned. Integer-type variables can also be assigned with declare.
FORMATdeclare variable=value |
declare name=Tommy
Option | Meaning |
---|---|
-f | Lists functions names and definitions. |
-r | Makes variables read-only. |
-x | Exports variable names to subshells. |
-i | Makes variables integer types. |
-a[a] | Treats variable as an array; i.e., assigns elements. |
-F[a] | Lists just function names. |
[a] a and -F are implemented only on versions of bash 2.x.
The scope of a variable refers to where the variable is visible within a program. For the shell, the scope of local variables is confined to the shell in which the variable is created.
When assigning a value, there can be no whitespace surrounding the equal sign. To set the variable to null, the equal sign is followed by a newline.[10]
A dollar sign is used in front of a variable to extract the value stored there.
The local function can be used to create local variables, but this is only used within functions. (See "Defining Functions" on page 693.)
Setting Local Variables. Local variables can be set by simply assigning a value to a variable name, or by using the declare built-in function as shown in Example 11.50.
1 $ round=world or declare round=world $ echo $round world 2 $ name="Peter Piper" $ echo $name Peter Piper 3 $ x= $ echo $x 4 $ file.bak="$HOME/junk" bash: file.bak=/home/jody/ellie/junk: not found
EXPLANATION
|
1 $ echo $$ 1313 2 $ round=world $ echo $round world 3 $ bash # Start a subshell 4 $ echo $$ 1326 5 $ echo $round 6 $ exit # Exits this shell, returns to parent shell 7 $ echo $$ 1313 8 $ echo $round world
EXPLANATION
|
Setting Read-Only Variables. A read-only variable is a special variable that cannot be redefined or unset. If, however, the declare function is used, a read-only variable can be redefined, but not unset.
1 $ name=Tom 2 $ readonly name $ echo $name Tom 3 $ unset name bash: unset: name: cannot unset: readonly variable 4 $ name=Joe bash: name: readonly variable 5 $ declare -r city='Santa Clara' 6 $ unset city bash: unset: city: cannot unset: readonly variable 7 $ declare city='San Francisco' # What happened here? $ echo $city San Francisco
EXPLANATION
|
Environment variables are available to the shell in which they are created and any subshells or processes spawned from that shell. They are often called global variables to differentiate them from local variables. By convention, environment variables are capitalized. Environment variables are variables that have been exported with the export built-in command.
The shell in which a variable is created is called the parent shell. If a new shell is started from the parent shell, it is called the child shell. Environment variables are passed to any child process started from the shell where the environment variables were created. They are passed from parent to child to grandchild, etc., but not the other direction; i.e., a child process can create an environment variable, but cannot pass it back to its parent, only to its children.[11] Some of the environment variables, such as HOME, LOGNAME, PATH, and SHELL, are set before you log on by the /bin/login program. Normally, environment variables are defined and stored in the .bash_profile file in the user's home directory. See Table 11.15 for a list of environment variables.
Setting Environment Variables. To set environment variables, the export command is used either after assigning a value or when the variable is set. The declare built-in, given the -x option, will do the same. (Do not use the $ on a variable when exporting it.)
FORMATexport variable=value variable=value; export variable declare -x variable=value |
export NAME=john PS1= '\d:\W:$USER> ' ; export PS1 declare -x TERM=sun
Option | Value |
---|---|
- - | Marks the end of option processing; the remaining parameters are arguments. |
-f | Name-value pairs are treated as functions, not variables. |
-n | Converts a global (exported) variable to a local variable. The variable will not be exported to child processes. |
-p | Displays all the global variables. |
1 $ export TERM=sun # or declare -x TERM=sun 2 $ NAME="John Smith" $ export NAME $ echo $NAME John Smith 3 $ echo $$ 319 # pid number for parent shell 4 $ bash # Start a subshell 5 $ echo $$ 340 # pid number for new shell 6 $ echo $NAME John Smith 7 $ declare -x NAME="April Jenner" $ echo $NAME April Jenner 8 $ exit # Exit the subshell and go back to parent shell 9 $ echo $$ 319 # pid number for parent shell 10 $ echo $NAME John Smith
EXPLANATION
|
Variable Name | Meaning |
---|---|
_ (underscore) | The last argument to the previous command. |
BASH | Expands to the full pathname used to invoke this instance of bash. |
BASH_ENV | Same as ENV but set only in bash versions 2.0 or above.[a] |
BASH_VERSINFO | Version information about this version of bash if the version is 2.0 or above.[a] |
BASH_VERSION | Expands to the version number of this instance of bash. |
CDPATH | The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is .:~:/usr. |
COLUMNS | If set, defines the width of the edit window for shell edit modes and the select command. |
DIRSTACK | The current contents of the directory stack if the bash version is 2.0 or above.[a] |
EDITOR | Pathname for a built-in editor: emacs, gmacs, or vi. |
ENV | The environment file that is executed every time a new bash shell is started, including a script. Normally the filename assigned to this variable is .bashrc. The value of ENV is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a pathname. |
EUID | Expands to the effective user ID of the current user, initialized at shell startup. |
FCEDIT | Default editor name for the fc command. |
FIGNORE | A colon-separated list of suffixes to ignore when performing filename completion. A filename whose suffix matches one of the entries in FIGNORE is excluded from the list of matched filenames. A sample value is .o:~. |
FORMAT | Used to format the output of the time reserved word on a command pipeline. |
GLOBIGNORE | A list of files that will be ignored during filename expansion (called globbing).[a] |
GROUPS | An array of groups to which the current user belongs.[a] |
HISTCMD | The history number, or index in the history list, of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset. |
HISTCONTROL | If set to a value of ignorespace, lines that begin with a space character are not entered on the history list. If set to a value of ignoredups, lines matching the last history line are not entered. A value of ignoreboth combines the two options. If unset, or if set to any other value than those above, all lines read by the parser are saved on the history list. |
HISTFILE | Specifies file in which to store command history. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits. |
HISTFILESIZE | The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines. The default value is 500. |
HISTSIZE | The number of commands to remember in the command history. The default value is 500. |
HOME | Home directory; used by cd when no directory is specified. |
HOSTFILE | Contains the name of a file in the same format as in /etc/hosts that should be read when the shell needs to complete a hostname. The file may be changed interactively; the next time hostname completion is attempted, bash adds the contents of the new file to the already existing database. |
HOSTTYPE | Automatically set to the type of machine on which bash is executing. The default is system-dependent. |
IFS | Internal field separators, normally SPACE, TAB, and NEWLINE, used for field splitting of words resulting from command substitution, lists in loop constructs, and reading input. |
IGNOREEOF | Controls the action of the shell on receipt of an EOF character as the sole input. If set, the value is the number of consecutive EOF characters typed as the first characters on an input line before bash exits. If the variable exists but does not have a numeric value, or has no value, the default value is 10. If it does not exist, EOF signifies the end of input to the shell. This is only in effect for interactive shells. |
INPUTRC | The filename for the readline startup file, overriding the default of ~./inputrc. |
LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_.[a] |
LC_ALL | Overrides the value of LANG and any other LC_ variable.[a] |
LC_COLLATE | Determines the collation order used when sorting the results of pathname expansion and the behavior of range expressions, equivalence classes, and collating sequences when matching pathnames and patterns.[a] |
LC_MESSAGES | Determines the locale used to translate double-quoted strings preceded by a $.[a] |
LINENO | Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function. |
MACHTYPE | Contains a string describing the system on which bash is executing.[a] |
If this parameter is set to the name of a mail file and the MAILPATH parameter is not set, the shell informs the user of the arrival of mail in the specified file. | |
MAIL_WARNING | If set, and a file that bash is checking for mail has been accessed since the last time it was checked, the message The mail in [filename where mail is stored] has been read is printed. |
MAILCHECK | This parameter specifies how often (in seconds) the shell will check for the arrival of mail in the files specified by the MAILPATH or MAIL parameters. The default value is 600 seconds (10 minutes). If set to zero, the shell will check before issuing each primary prompt. |
MAILPATH | A colon-separated list of filenames. If this parameter is set, the shell informs the user of the arrival of mail in any of the specified files. Each filename can be followed by a % and a message that will be printed when the modification time changes. The default message is You have mail. |
OLDPWD | Last working directory. |
OPTARG | The value of the last option argument processed by the getopts built-in command. |
OPTERR | If set to 1, displays error messages from the getopts built-in. |
OPTIND | The index of the next argument to be processed by the getopts built-in command. |
OSTYPE | Automatically set to a string that describes the operating system on which bash is executing. The default is system-dependent. |
PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. The default path is system-dependent, and is set by the administrator who installs bash. A common value is /usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:. |
PIPESTATUS | An array containing a list of exit status values from processes in the most recently executed foreground jobs in a pipeline. |
PPID | Process ID of the parent process. |
PROMPT_COMMAND | The command assigned to this variable is executed before the primary prompt is displayed. |
PS1 | Primary prompt string, by default $. |
PS2 | Secondary prompt string, by default >. |
PS3 | Selection prompt string used with the select command, by default #?. |
PS4 | Debug prompt string used when tracing is turned on, by default +. Tracing can be turned on with set -x. |
PWD | Present working directory; set by cd. |
RANDOM | Each time this parameter is referenced, a random integer is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset. |
REPLY | Set when read is not supplied arguments. |
SECONDS | Each time SECONDS is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset. |
SHELL | When the shell is invoked, it scans the environment for this name. The shell gives default values to PATH, PS1, PS2, MAILCHECK, and IFS. HOME and MAIL are set by login(1). |
SHELLOPTS | Contains a list of enabled shell options, such as braceexpand, hashall, monitor, etc. |
SHLVL | Incremented by one each time an instance of bash is started. |
TMOUT | Specifies number of seconds to wait for input before exiting. |
UID | Expands to the user ID of the current user, initialized at shell startup. |
[a] Not available in bash versions prior to 2.x.
Unsetting Variables. Both local and environment variables can be unset by using the unset command, unless the variables are set as read-only.
unset name; unset TERM
EXPLANATIONThe unset command removes the variable from the shell's memory. |
Printing the Values of Variables: The echo Command. The built-in echo command prints its arguments to standard output. Echo, with the -e option, allows the use of numerous escape sequences that control the appearance of the output. Table 11.16 lists the echo options and escape sequences.
Option | Meaning |
---|---|
-e | Allows interpretation of the escape sequences shown below. |
-n | Suppresses newline at the end of a line of output. |
-E[a] | Disables the interpretation of these escape characters, even on systems where they are interpreted by default (bash 2.x). |
Escape Sequence | |
\a[a] | Alert (bell). |
\b | Backspace. |
\c | Prints the line without a newline. |
\f | Form feed. |
\n | Newline. |
\r | Return. |
\t | Tab. |
\v | Vertical tab. |
\\ | Backslash. |
\nnn | The character whose ASCII code is nnn (octal). |
[a] Not available in bash versions prior to 2.x.
When using the escape sequences, don't forget to use the -e switch!
1 $ echo The username is $LOGNAME. The username is ellie. 2 $ echo -e "\t\tHello there\c" Hello there$ 3 $ echo -n "Hello there" Hello there$
EXPLANATION
|
The printf Command. The GNU version of printf[12] can be used to format printed output. It prints the formatted string in the same way as the C printf function. The format consists of a string that may contain formatting instructions to describe how the printed output will look. The formatting instructions are designated with a % followed by specifiers (diouxXfeEgGcs) where %f would represent a floating point number and %d would represent a whole (decimal) number.
To see a complete listing of printf specifiers and how to use them, type at the command line prompt: printf - -help. To see what version of printf you are using, type: printf - -version. If you are using bash 2.x, the built-in printf command uses the same format as the executable version in /usr/bin.
FORMATprintf format [argument...] |
printf "%10.2f%5d\n" 10.5 25
Format Specifier | Value |
---|---|
\" | Double quote. |
\0NNN | An octal character where NNN represents 0 to 3 digits. |
\\ | Backslash. |
\a | Alert or beep. |
\b | Backspace. |
\c | Produce no further output. |
\f | Form feed. |
\n | Newline. |
\r | Return. |
\t | Horizontal tab. |
\v | Vertical tab. |
\xNNN | Hexadecimal character, where NNN is 1 to 3 digits. |
%% | A single %. |
%b | Argument as a string with \ escapes interpreted. |
1 $ printf --version printf (GNU sh-utils) 1.16 2 $ type printf printf is a shell builtin 3 $ printf "The number is %.2f\n" 100 The number is 100.00 4 $ printf "%-20s%-15s%10.2f\n" "Jody" "Savage" 28 Jody Savage 28.00 5 $ printf "|%-20s|%-15s|%10.2f|\n" "Jody" "Savage" 28 Jody |Savage | 28.00| 6 $ printf "%s's average was %.1f%%.\n" "Jody" $(( (80+70+90)/3 )) Jody's average was 80.0%.
EXPLANATION
|
Variable Expansion Modifiers (Parameter Expansion). Variables can be tested and modified by using special modifiers. The modifier provides a shortcut conditional test to check if a variable has been set, and then assigns a value to the variable based on the outcome of the test. See Table 11.18 for a list of variable modifiers.
Modifier | Value |
---|---|
${variable:-word} | If variable is set and is non-null, substitute its value; otherwise, substitute word. |
${variable:=word} | If variable is set or is non-null, substitute its value; otherwise, set it to word. The value of variable is substituted permanently. Positional parameters may not be assigned in this way. |
${variable:+word} | If variable is set and is non-null, substitute word; otherwise, substitute nothing. |
${variable:?word} | If variable is set and is non-null, substitute its value; otherwise, print word and exit from the shell. If word is omitted, the message parameter null or not set is printed. |
${variable:offset} | Gets the substring of the value in variable starting at offset, where offset starts at 0 to the end of the string.[a] |
${variable:offset:length} | Gets the substring of the value in variable starting at offset, length characters over. |
[a] Not available on bash versions prior to 2.0.
Using the colon with any of the modifiers (-, =, +, ?) checks whether the variable is not set or is null; without the colon, a variable set to null is considered to be set.
(Substitute Temporary Default Values) 1 $ fruit=peach 2 $ echo ${fruit:-plum} peach 3 $ echo ${newfruit:-apple} apple 4 $ echo $newfruit 5 $ echo $EDITOR # More realistic example 6 $ echo ${EDITOR:-/bin/vi} /bin/vi 7 $ echo $EDITOR 8 $ name= $ echo ${name-Joe} 9 $ echo ${name:-Joe} Joe
EXPLANATION
|
(Substitute Permanent Default Values) 1 $ name= 2 $ echo ${name:=Peter} Peter 3 $ echo $name Peter 4 $ echo ${EDITOR:=/bin/vi} /bin/vi 5 $ echo $EDITOR /bin/vi
EXPLANATION
|
(Substitute Temporary Alternate Value) 1 $ foo=grapes 2 $ echo ${foo:+pears} pears 3 $ echo $foo grapes $
EXPLANATION
|
(Creating Error Messages Based On Default Values) 1 $ echo ${namex:?"namex is undefined"} namex: namex is undefined 2 $ echo ${y?} y: parameter null or not set
EXPLANATION
|
(Creating Substring[a]) 1 $ var=notebook 2 $ echo ${var:0:4} note 3 $ echo ${var:4:4} book 4 $ echo ${var:0:2} no
[a] Not available in versions of bash prior to 2.x.
EXPLANATION
|
Variable Expansion of Substrings. Pattern-matching arguments are used to strip off certain portions of a string from either the front or end of the string. The most common use for these operators is stripping off pathname elements from the head or tail of the path. See Table 11.19.
Expression | Function |
---|---|
${variable%pattern} | Matches the smallest trailing portion of the value of variable to pattern and removes it. |
${variable%%pattern} | Matches the largest trailing portion of the value of variable to pattern and removes it. |
${variable#pattern} | Matches the smallest leading portion of the value of variable to pattern and removes it. |
${variable##pattern} | Matches the largest leading portion of the value of variable to pattern and removes it. |
${#variable} | Substitutes the number of characters in the variable. If * or @, the length is the number of positional parameters. |
[a] Not available on versions of bash prior to 2.x.
1 $ pathname="/usr/bin/local/bin" 2 $ echo ${pathname%/bin*} /usr/bin/local
EXPLANATION
|
1 $ pathname="usr/bin/local/bin" 2 $ echo ${pathname%%/bin*} /usr
EXPLANATION
|
1 $ pathname=/home/lilliput/jake/.bashrc 2 $ echo ${pathname#/home} /lilliput/jake/.bashrc
EXPLANATION
|
1 $ pathname=/home/liliput/jake/.bashrc 2 $ echo ${pathname##*/} .bashrc
EXPLANATION
|
1 $ name="Ebenezer Scrooge" 2 $ echo ${#name} 16
EXPLANATION
|
Positional Parameters. Normally, the special built-in variables, often called positional parameters, are used in shell scripts when passing arguments from the command line, or used in functions to hold the value of arguments passed to the function. The variables are called positional parameters because they are referenced by numbers 1, 2, 3, and so on, representing their respective positions in the parameter list. See Table 11.20.
The name of the shell script is stored in the $0 variable. The positional parameters can be set, reset, and unset with the set command.
Expression | Function |
---|---|
$0 | References the name of the current shell script. |
$1 $9 | Positional parameters 1 9. |
${10} | Positional parameter 10. |
$# | Evaluates to the number of positional parameters. |
$* | Evaluates to all the positional parameters. |
$@ | Same as $*, except when double quoted. |
"$*" | Evaluates to "$1 $2 $3", etc. |
"$@" | Evaluates to "$1" "$2" "$3", etc. |
1 $ set punky tommy bert jody $ echo $* # Prints all the positional parameters punky tommy bert jody 2 $ echo $1 # Prints the first position punky 3 $ echo $2 $3 # Prints the second and third position tommy bert 4 $ echo $# # Prints the total number of positional 4 # parameters 5 $ set a b c d e f g h i j k l m $ print $10 # Prints the first positional parameter a0 # followed by a 0. $ echo ${10} ${11} # Prints the 10th and 11th positions j k 6 $ echo $# 13 7 $ echo $* a b c d e f g h i j k l m 8 $ set file1 file2 file3 $ echo \$$# $3 9 $ eval echo \$$# file3 10 $ set -- # Unsets all positional parameters
EXPLANATION
|
Other Special Variables. The shell has special variables consisting of a single character.The dollar sign preceding the character allows you to access the value stored in the variable. See Table 11.21.
Variable | Meaning |
---|---|
$ | The PID of the shell. |
- | The sh options currently set. |
? | The exit value of last executed command. |
! | The PID of the last job put in the background. |
1 $ echo The pid of this shell is $$ The pid of this shell is 4725 2 $ echo The options for this shell are $- The options for this shell are imh 3 $ grep dodo /etc/passwd $ echo $? 1 4 $ sleep 25& 4736 $ echo $! 4736
EXPLANATION
|
Quoting is used to protect special metacharacters from interpretation and prevent parameter expansion. There are three methods of quoting: the backslash, single quotes, and double quotes. The characters listed in Table 11.22 are special to the shell and must be quoted.
Metacharacter | Meaning |
---|---|
; | Command separator. |
& | Background processing. |
( ) | Command grouping; creates a subshell. |
{ } | Command grouping; does not create a subshell. |
| | Pipe. |
< | Input redirection. |
> | Output redirection. |
newline | Command termination. |
space/tab | Word delimiter. |
$ | Variable substitution character. |
* [ ] ? | Shell metacharacters for filename expansion. |
Single and double quotes must be matched. Single quotes protect special metacharacters, such as $, *, ?, |, >, and <, from interpretation. Double quotes also protect special metacharacters from being interpreted, but allow variable and command substitution characters (the dollar sign and backquotes) to be processed. Single quotes will protect double quotes and double quotes will protect single quotes.
Unlike the Bourne shell, bash tries to let you know if you have mismatched quotes. If running interactively, a secondary prompt appears when quotes are not matched; if in a shell script, the file is scanned and if the quote is not matched, the shell will attempt to match it with the next available quote. If the shell cannot match it with the next available quote, the program aborts and the message bash:unexpected EOF while looking for '"' appears on the terminal. Quoting can be a real hassle for even the best of shell programmers! See Appendix C for shell quoting rules.
The Backslash. The backslash is used to quote (or escape) a single character from interpretation. The backslash is not interpreted if placed in single quotes. The backslash will protect the dollar sign ($), backquotes (' '), and the backslash from interpretation if enclosed in double quotes.
1 $ echo Where are you going\? Where are you going? 2 $ echo Start on this line and \ > go to the next line. Start on this line and go to the next line. 3 $ echo \\ \ 4 $ echo '\\' \\ 5 $ echo '\$5.00' \$5.00 6 $ echo "\$5.00" $5.00 7 $ echo 'Don\'t you need $5.00?' > >' Don\t you need .00?
EXPLANATION
|
Single Quotes. Single quotes must be matched. They protect all metacharacters from interpretation. To print a single quote, it must be enclosed in double quotes or escaped with a backslash.
1 $ echo 'hi there > how are you? > When will this end? > When the quote is matched > oh' hi there how are you? When will this end? When the quote is matched oh 2 $ echo Don\'t you need '$5.00?' Don't you need $5.00? 3 $ echo 'Mother yelled, "Time to eat!"' Mother yelled, "Time to eat!"
EXPLANATION
|
Double Quotes. Double quotes must be matched, will allow variable and command substitution, and protect any other special metacharacters from being interpreted by the shell.
1 $ name=Jody 2 $ echo "Hi $name, I'm glad to meet you!" Hi Jody, I'm glad to meet you! 3 $ echo "Hey $name, the time is $(date)" Hey Jody, the time is Wed Jul 14 14:04:11 PST 2001
EXPLANATION
|
Command substitution is used when assigning the output of a command to a variable or when substituting the output of a command within a string. All shells use backquotes to perform command substitution.[13] Bash allows two forms: the older form, where the command(s) is placed within backquotes, and the new Korn-style form, where the command(s) is placed within a set of parentheses preceded by a dollar sign.
Bash performs the expansion by executing the command and returning the standard output of the command, with any trailing newlines deleted. When the old-style backquote form of substitution is used, the backslash retains its literal meaning except when followed by $, ', or \. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the old form, the inner backquotes must be escaped with backslashes.
FORMAT'UNIX command' # Old method with backquotes $(UNIX command) # New method |
(The Old Way) 1 $ echo "The hour is 'date +%H'" The hour is 09 2 $ name='awk -F: '{print $1}' database' $ echo $name Ebenezer Scrooge 3 $ ls 'ls /etc' shutdown 4 $ set 'date' 5 $ echo $* Wed Jul 14 09:35:21 PDT 2001 6 $ echo $2 $6 Jul 2001 7 $ echo 'basename \'pwd\'' ellie
EXPLANATION
|
The bash alternate for using backquotes in command substitution is presented below in Example 11.75.
(The New Way) 1 $ d=$(date) $ echo $d Wed Jul 14 09:35:21 PDT 2001 2 $ lines = $(cat filex) 3 $ echo The time is $(date +%H) The time is 09 4 $ machine=$(uname -n) $ echo $machine jody 5 $ pwd /usr/local/bin $ dirname="$(basename $(pwd)) " # Nesting commands $ echo $dirname bin 6 $ echo $(cal) # Newlines are lost July 2001 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 7 $ echo "$(cal)" July 2001 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
EXPLANATION
|
The shell performs arithmetic expansion by evaluating an arithmetic expression and substituting the result. The expression is treated as if it were in double quotes and the expressions may be nested. For a complete discussion of arithmetic operations, and arithmetic evaluations, see "The let Command" on page 732.
There are two formats for evaluating arithmetic expressions:
FORMAT$[ expression ] $(( expression )) |
echo $[ 5 + 4 - 2 ] 7 echo $[ 5 + 3 * 2] 11 echo $[(5 + 3) * 2] 16 echo $(( 5 + 4 )) 9 echo $(( 5 / 0 )) bash: 5/0: division by 0 ( error token is "0")
When you are performing the expansion of variables, commands, arithmetic expressions, and pathnames, the shell is programmed to follow a specific order when scanning the command line. Assuming that the variables are not quoted, the processing is performed in the following order:
Brace expansion
Tilde expansion
Parameter expansion
Variable substitution
Command substitution
Arithmetic expansion
Word splitting
Pathname expansion
Versions of bash 2.x, provide for creation of one-dimensional arrays. Arrays allow you to collect a list of words into one variable name, such as a list of numbers, a list of names, or a list of files. Arrays are created with the built-in function declare -a, or can be created on the fly by giving a subscript to a variable name, such as x[0]=5. The index value is an integer starting at 0. There is no maximum size limit on the array, and indices do not have to be ordered numbers, i.e., x[0], x[1], x[2] . To extract an element of an array, the syntax is ${arrayname[index]}. If declare is given the -a and -r options, a read-only array is created.
FORMATdeclare -a variable_name variable = ( item1 item2 item3 ... ) |
declare -a nums=(45 33 100 65) declare -ar names (array is readonly) names=( Tom Dick Harry) states=( ME [3]=CA CT ) x[0]=55 n[4]=100
When assigning values to an array, they are automatically started at index 0 and incremented by 1 for each additional element added. You do not have to provide indices in an assignment, and if you do, they do not have to be in order. To unset an array, use the unset command followed by the array name, and to unset one element of the array, use unset and the arrayname[subscript] syntax.
The declare, local, and read-only built-ins also can take the -a option to declare an array. The read command with the -a option is used to read in a list of words from standard input into array elements.
1 $ declare -a friends 2 $ friends=(Sheryl Peter Louise) 3 $ echo ${friends[0]} Sheryl 4 $ echo ${friends[1]} Peter 5 $ echo ${friends[2]} Louise 6 $ echo "All the friends are ${friends[*]}" All the friends are Sheryl Peter Louise 7 $ echo "The number of elements in the array is ${#friends[*]}" The number of elements in the array is 3 8 $ unset friends or unset ${friends[*]}
EXPLANATION
|
1 $ x[3]=100 $ echo ${x[*]} 100 2 $ echo ${x[0]} 3 $ echo ${x[3]} 100 4 $ states=(ME [3]=CA [2]=CT) $ echo ${states[*]} ME CA CT 5 $ echo ${states[0]} ME 6 $ echo ${states[1]} 7 $ echo ${states[2]} CT 8 $ echo ${states[3]} CA
EXPLANATION
|
Bash functions are used to execute a group of commands with a name within the context of the current shell (a child process is not forked). They are like scripts, only more efficient. Once defined, functions become part of the shell's memory so that when the function is called, the shell does not have to read it in from the disk as it does with a file. Often functions are used to improve the modularity of a script. Once defined, functions can be used again and again. Although functions can be defined at the prompt when running interactively, they are often defined in the user's initialization file, .bash_profile. They must be defined before they are invoked.
Defining Functions. There are two ways to declare a bash function. One way, the old Bourne shell way, is to give the function name followed by a set of empty parentheses, followed by the function definition. The new way (Korn shell way) is to use the function keyword followed by the function name and then the function definition. If using the new way, the parentheses are optional. The function definition is enclosed in curly braces. It consists of commands separated by semicolons. The last command is terminated with a semicolon. Spaces around the curly braces are required. Any arguments passed to the function are treated as positional parameters within the function. The positional parameters in a function are local to the function. The local built-in function allows local variables to be created within the function definition. Functions may also be recursive, i.e., call themselves an unlimited number of times.
FORMATfunction_name () { commands ; commands; } function function_name { commands ; commands; } function function_name () { commands ; commands; } |
1 $ function greet { echo "Hello $LOGNAME, today is $(date)"; } 2 $ greet Hello ellie, today is Wed Jul 14 14:56:31 PDT 2001 3 $ greet () { echo "Hello $LOGNAME, today is $(date)"; } 4 $ greet Hello ellie, today is Wed Jul 14 15:16:22 PDT 2001 5 $ declare -f declare -f greet() { echo "Hello $LOGNAME, today is $(date)" } 6 $ declare -F[a] declare -f greet 7 $ export -f greet 8 $ bash Start subshell 9 $ greet Hello ellie, today is Wed Jul 14 17:59:24 PDT 2001
[a] Only on bash version 2.x.
EXPLANATION
|
1 $ function fun { echo "The current working directory is $PWD." echo "Here is a list of your files: " ls echo "Today is $(date +%A)."; } 2 $ fun The current working directory is /home. Here is a list of your files: abc abc123 file1.bak none nothing tmp abc1 abc2 file2 nonsense nowhere touch abc122 file1 file2.bak noone one Today is Wednesday. 3 $ function welcome { echo "Hi $1 and $2"; } 4 $ welcome tom joe Hi tom and joe 5 $ set jane anna lizzy 6 $ echo $* jane anna lizzy 7 $ welcome johan joe hi johan and joe 8 $ echo $1 $2 johan joe 9 $ unset -f welcome # unsets the function
EXPLANATION
|
Listing and Unsetting Functions. To list functions and their definitions, use the declare command. In bash versions 2.x and above, declare -F lists just function names. The function and its definition will appear in the output, along with the exported and local variables. Functions and their definitions are unset with the unset -f command.
When the shell starts up, it inherits three files: stdin, stdout, and stderr. Standard input normally comes from the keyboard. Standard output and standard error normally go to the screen. There are times when you want to read input from a file or send output or errors to a file. This can be accomplished by using I/O redirection. See Table 11.23 for a list of redirection operators.
Redirection Operator | What It Does |
---|---|
< | Redirects input. |
> | Redirects output. |
>> | Appends output. |
2> | Redirects error. |
&> | Redirects output and error. |
>& | Redirects output and error (preferred way). |
2>&1 | Redirects error to where output is going. |
1>&2 | Redirects output to where error is going. |
>| | Overrides noclobber when redirecting output. |
<> filename | Uses file as both standard input and output if a device file (from /dev). |
1 $ tr '[A-Z]' '[a-z]' < myfile # Redirect input 2 $ ls > lsfile # Redirect output $ cat lsfile dir1 dir2 file1 file2 file3 3 $ date >> lsfile # Redirect and append otuput $ cat lsfile dir1 dir2 file1 file2 file3 Sun Sept 17 12:57:22 PDT 2001 4 $ cc prog.c 2> errfile # Redirect error 5 $ find . -name \*.c -print > foundit 2> /dev/null # Redirect output to foundit and errors to /dev/null, # respectively. 6 $ find . -name \*.c -print >& foundit # Redirect both output and errors to foundit. 7 $ find . -name \*.c -print > foundit 2>&1 # Redirect output to foundit and send errors to where output # is going; i.e. foundit 8 $ echo "File needs an argument" 1>&2 # Send standard output to error
EXPLANATION
|
The exec Command and Redirection. The exec command can be used to replace the current program with a new one without starting a new process. Standard output or input can be changed with the exec command without creating a subshell. (See Table 11.24.) If a file is opened with exec, subsequent read commands will move the file pointer down the file a line at a time until the end of the file. The file must be closed to start reading from the beginning again. However, if using UNIX utilities such as cat and sort, the operating system closes the file after each command has completed.
exec Command | What It Does |
---|---|
exec ls | ls executes in place of the shell. When ls is finished, the shell in which it was started does not return. |
exec < filea | Opens filea for reading standard input. |
exec > filex | Opens filex for writing standard output. |
exec 3< datfile | Opens datfile as file descriptor 3 for reading input. |
sort <&3 | Datfile is sorted. |
exec 4>newfile | Opens newfile as file descriptor (fd) 4 for writing. |
ls >&4 | Output of ls is redirected to newfile. |
exec 5<&4 | Makes fd 5 a copy of fd 4. |
exec 3<&- | Closes fd 3. |
1 $ exec date Thu Oct 14 10:07:34 PDT 2001 <Login prompt appears if you are in your login shell > 2 $ exec > temp $ ls $ pwd $ echo Hello 3 $ exec > /dev/tty 4 $ echo Hello Hello
EXPLANATION
|
1 > bash 2 $ cat doit pwd echo hello date 3 $ exec < doit /home/homebound/ellie/shell hello Thu Oct 14 10:07:34 PDT 2001 4 >
EXPLANATION
|
1 $ exec 3> filex 2 $ who >& 3 3 $ date >& 3 4 $ exec 3>&- 5 $ exec 3<filex 6 $ cat <&3 ellie tty Jul 21 09:50 ellie ttyp1 Jul 21 11:16 (:0.0) ellie ttyp0 Jul 21 16:49 (:0.0) Wed Jul 21 17:15:18 PDT 2001 7 $ exec 3<&- 8 $ date >& 3 date: write error: Bad file descriptor
EXPLANATION
|
A pipe takes the output from the command on the left-hand side of the pipe symbol and sends it to the input of the command on the right-hand side of the pipe symbol. A pipeline can consist of more than one pipe.
The purpose of the commands in Example 11.86 is to count the number of people logged on (who), save the output of the command in a file (tmp), use the wc -l to count the number of lines in the tmp file (wc -l), and then remove the tmp file (i.e., find the number of people logged on).
1 $ who > tmp 2 $ wc -l tmp 4 tmp 3 $ rm tmp # Using a pipe saves disk space and time. 4 $ who | wc -l 4 5 $ du .. | sort -n | sed -n '$p' 1980 6 $ ( du / | sort -n | sed -n '$p' ) 2> /dev/null 1057747 /
EXPLANATION
|
The here document is a special form of quoting. It accepts inline text for a program expecting input, such as mail, sort, or cat, until a user-defined terminator is reached. It is often used in shell scripts for creating menus. The command receiving the input is appended with a << symbol, followed by a user-defined word or symbol, and a newline. The next lines of text will be the lines of input to be sent to the command. The input is terminated when the user-defined word or symbol is then placed on a line by itself in the leftmost column (it cannot have spaces surrounding it). The word is used in place of Control-D to stop the program from reading input.
If the terminator is preceded by the <<- operator, leading tabs, and only tabs, may precede the final terminator. The user-defined terminating word or symbol must match exactly from "here" to "here." The following examples illustrate the use of the here document at the command line to demonstrate the syntax. It is much more practical to use them in scripts.
EXPLANATION
|
1 $ cat <<- DONE > Hello there > What's up? >Bye now The time is 'date'. 2 > DONE Hello there What's up? Bye now The time is Sun Feb 819:48:23 PST 2001. $
EXPLANATION
|
When the shell is started using the bash command, it can take options to modify its behavior. There are two types of options: single-character options and multicharacter options. The single-character options consist of a single leading dash followed by a single character. The multicharacter options consist of two leading dashes and any number of characters. Multicharacter options must appear before single-character options. An interactive login shell normally starts up with -i (starts an interactive shell) , -s (reads from standard input), and -m (enables job control). See Table 11.25.
Option | Meaning |
---|---|
-c string | Commands are read from string. Any arguments after string are assigned to positional parameters, starting at $0. |
-D | A list of double quoted strings, preceded by a $, are printed to standard output. These strings are subject to language translation when the current locale is not C or POSIX.The -n option is implied; no commands will be executed. |
-i | Shell is in the interactive mode. TERM, QUIT, and INTERRUPT are ignored. |
-s | Commands are read from standard input and allows the setting of positional parameters. |
-r | Starts a restricted shell. |
- - | Signals the end of options and disables further option processing. Any arguments after - - or - are treated as filenames and arguments. |
- -dump-strings | Same as -D. |
- -help | Displays a usage message for a built-in command and exits. |
- -login | Causes bash to be invoked as a login shell. |
- -noediting | When bash is running interactively, does not use the Readline library. |
- -noprofile | When starting up, bash does not read the initialization files: /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile. |
- -norc | For interactive shells, bash will not read the ~/.bashrc file. Turned on by default, if running shell as sh. |
- -posix | Changes the behavior of bash to match the POSIX 1003.2 standard, if otherwise it wouldn't. |
- -quiet | Displays no information at shell startup, the default. |
- -rcfile file | If bash is interactive, uses this intialization file instead of ~/.bashrc. |
- -restricted | Starts a restricted shell. |
- -verbose | Turns on verbose; same as -v. |
- -version | Displays version information about this bash shell and exits. |
The set command can be used to turn shell options on and off, as well as for handling command line arguments. To turn an option on, the dash ( ) is prepended to the option; to turn an option off, the plus sign (+) is prepended to the option. See Table 11.26 for a list of set options.
1 $ set -f 2 $ echo * * 3 $ echo ?? ?? 4 $ set +f
EXPLANATION
|
Name of Option | Shortcut Switch | What It Does |
---|---|---|
allexport | -a | Automatically marks new or modified variables for export from the time the option is set, until unset. |
braceexpand | -B | Enables brace expansion, and is a default setting. |
emacs | For command line editing, uses the emacs built-in editor, and is a default setting. | |
errexit | -e | If a command returns a nonzero exit status (fails), exits. Not set when reading initialization files. |
histexpand | -H | Enables ! and !! when performing history substitution, and is a default setting. |
history | Enables command line history; on by default. | |
ignoreeof | Disables EOF (Control-D) from exiting a shell; must type exit. Same as setting shell variable, IGNOREEOF=10. | |
keyword | -k | Places keyword arguments in the environment for a command. |
interactive_comments | For interactive shells, a leading # is used to comment out any text remaining on the line. | |
monitor | -m | Allows job control. |
noclobber | -C | Protects files from being overwritten when redirection is used. |
noexec | -n | Reads commands, but does not execute them. Used to check the syntax of scripts. Not on when running interactively. |
noglob | -d | Disables pathname expansion; i.e., turns off wildcards. |
notify | -b | Notifies user when background job finishes. |
nounset | -u | Displays an error when expanding a variable that has not been set. |
onecmd | -t | Exits after reading and executing one command. |
physical | -P | If set, does not follow symbolic links when typing cd or pwd. The physical directory is used instead. |
posix | Shell behavior is changed if the default operation doesn't match the POSIX standard. | |
privileged | -p | When set, the shell does not read the .profile or ENV file and shell functions are not inherited from the environment; automatically set for setuid scripts. |
verbose | -v | Turns on the verbose mode for debugging. |
vi | For command line editing, uses the vi built-in editor. | |
xtrace | -x | Turns on the echo mode for debugging. |
The shopt (bash 2.x) command can also be used to turn shell options on and off.
Option | Meaning |
---|---|
cdable_vars | If an argument to the cd built-in command is not a directory, it is assumed to be the name of a variable whose value is the directory to change to. |
cdspell | Corrects minor errors in the spelling of a directory name in a cd command. The errors checked for are transposed characters, a missing character, and a character too many. If a correction is found, the corrected path is printed, and the command proceeds. Only used by interactive shells. |
checkhash | Bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed. |
checkwinsize | Bash checks the window size after each command and, if necessary, updates the values of LINES and COLUMNS. |
cmdhist | Bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multiline commands. |
dotglob | Bash includes filenames beginning with a dot (.) in the results of filename expansion. |
execfail | A noninteractive shell will not exit if it cannot execute the file specified as an argument to the exec built-in command. An interactive shell does not exit if exec fails. |
expand_aliases | Aliases are expanded. Enabled by default. |
extglob | The extended pattern matching features (regular expression metacharacters derived from Korn shell for filename expansion) are enabled. |
histappend | The history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file. |
histreedit | If readline is being used, a user is given the opportunity to re-edit a failed history substitution. |
histverify | If set, and readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification. |
hostcomplete | If set, and readline is being used, bash will attempt to perform hostname completion when a word containing @ is being completed. Enabled by default. |
huponexit | If set, bash will send SIGHUP (hangup signal) to all jobs when an interactive login shell exits. |
interactive_comments | Allows a word beginning with # to cause that word and all remaining characters on that line to be ignored in an interactive shell. Enabled by default. |
lithist | If enabled, and the cmdhist option is enabled, multiline commands are saved to the history with embedded newlines rather than using semicolon separators where possible. |
mailwarn | If set, and a file that bash is checking for mail has been accessed since the last time it was checked, the message The mail in mailfile has been read is displayed. |
nocaseglob | If set, bash matches filenames in a case-insensitive fashion when performing filename expansion. |
nullglob | If set, bash allows filename patterns that match no files to expand to a null string, rather than themselves. |
promptvars | If set, prompt strings undergo variable and parameter expansion after being expanded. Enabled by default. |
restricted_shell | The shell sets this option if it is started in restricted mode. The value may not be changed. This is not reset when the startup files are executed, allowing the startup files to discover whether or not a shell is restricted. |
shift_verbose | If this is set, the shift built-in prints an error message when the shift count exceeds the number of positional parameters. |
sourcepath | If set, the source built-in uses the value of PATH to find the directory containing the file supplied as an argument. Enabled by default. |
source | A synonym for dot (.). |
The shell has a number of commands that are built-in to its source code. Because the commands are built-in, the shell doesn't have to locate them on disk, making execution much faster. The help feature provided with bash gives you online help for any built-in command. The built-in commands are listed in Table 11.28.
Command | What It Does |
---|---|
: | Do-nothing command; returns exit status zero. |
. file | The dot command reads and executes command from file. |
break [n] | See "Loop Control". |
. | Executes program in context of current process; same as source. |
alias | Lists and creates "nicknames" for existing commands. |
bg | Puts a job in the background. |
bind | Display current key and function bindings, or binds keys to a readline function or macro. |
break | Breaks out of the innermost loop. |
builtin [sh-builtin [args]] | Runs a shell built-in, passing it args and returning 0 exit status. Useful if a function and built-in have the same name. |
cd [arg] | Changes the directory to home if no arg or to value of arg. |
command command [arg] | Runs a command even if a function has the same name; i.e., bypasses function lookup. |
continue [n] | See "Loop Control". |
declare [var] | Displays all variables or declares variables with optional attributes. |
dirs | Displays a list of currently remembered directories resulting from pushd. |
disown | Removes an active job from the job table. |
echo [args] | Displays args terminated with a newline. |
enable | Enables and disables shell built-in commands. |
eval [args] | Reads args as input to the shell and executes the resulting command(s). |
exec command | Runs command in place of this shell. |
exit [n] | Exits the shell with status n. |
export [var] | Makes var known to subshells. |
fc | History's fix command for editing history commands. |
fg | Puts background job into foreground. |
getopts | Parses and processes command line options. |
hash | Controls the internal hash table for quicker searches for commands. |
help [command] | Displays helpful info about built-in commands and, if command is specified, detailed help about that built-in command. |
history | Displays the history list with line numbers. |
jobs | Lists jobs put in the background. |
kill [-signal process ] | Sends the signal to the PID number or job number of the process. Type at the prompt: kill -l. |
getopts | Used in shell scripts to parse command line and check for legal options. |
let | Used for evaluating arithmetic expressions and assigning results of arithmetic calculations to variables. |
local | Used in functions to restrict the scope of variables to the function. |
logout | Exits the login shell. |
popd | Removes entries from the directory stack. |
pushd | Adds entries to the directory stack. |
pwd | Prints present working directory. |
read [var] | Reads line from standard input into variable var. |
readonly [var] | Makes variable var read-only. Cannot be reset. |
return [n] | Returns from a function where n is the exit value given to the return. |
set | Sets options and positional parameters. See "The set Command and Positional Parameters". |
shift [n] | Shifts positional parameters to the left n times. |
stop pid | Halts execution of the process number PID. |
suspend | Stops execution of the current shell (but not if a login shell). |
test | Checks file types and evaluates conditional expressions. |
times | Prints accumulated user and system times for processes run from this shell. |
trap [arg] [n] | When shell receives signal n ( 0, 1, 2, or 15 ), executes arg. |
type [command] | Prints the type of command; e.g., pwd is a built-in shell command. |
typeset | Same as declare. Sets variables and gives them attributes. |
ulimit | Diplays and sets process resource limits. |
umask [octal digits] | Sets user file creation mode mask for owner, group, and others. |
unalias | Unsets aliases. |
unset [name] | Unset value of variable or function. |
wait [pid#n] | Waits for background process with PID number n and reports termination status. |
1: | What process puts the login prompt on your screen? |
2: | What process assigns values to HOME, LOGNAME, and PATH? |
3: | How do you know what shell you are using? |
4: | What command will allow you to change your login shell? |
5: | Where is your login shell assigned? (What file?) |
6: | Explain the difference between the /etc/profile and ~/.bash_profile file. |
7: | Which one is executed first? |
8: | Edit your .bash_profile file as follows:
|
9: | What is the BASH_ENV file? When is it executed? |
10: | What is the default primary prompt?
|
11: | Explain the function of each of the following settings:
|
12: | In what file are the settings in the previous example stored? Why are they stored there? |
13: | What does shopt -p do? Why use shopt instead of set? |
14: | What is a built-in command? How can you tell if a command is a built-in or an executable? What is the purpose of the builtin command? The enable command? |
15: | What would cause the shell to return an exit status of 127? |
1: | What is the difference between a program and a process? What is a job? |
2: | What is the PID of your shell? |
3: | How do you stop a job? |
4: | What command brings a background job into the foreground? |
5: | How do you list all running jobs? All stopped jobs? |
6: | What is the purpose of the kill command? |
7: | What does jobs -l display? What does kill -l display? |
1: | What is filename completion? |
2: | What is the name of the file that stores a history of commands entered at the command line? |
3: | What does the HISTSIZE variable control? What does HISTFILESIZE control? |
4: | What does bang, bang mean? |
5: | How would you reexecute the last command that started with a v? |
6: | How would you reexecute the 125th command? How would you print the history list in reverse? |
7: | How do you set interactive editing to use the vi editor? In what initialization file would you put this setting? |
8: | What is the fc command? |
9: | What is the purpose of the Readline library? From what initialization file does it read instructions? |
10: | What is key binding? How do you find out what keys are bound? |
11: | What is the universal argument? |
12: | Create an alias for the following commands:
|
1: | Make a directory called wildcards. Cd to that directory and type at the prompt: touch ab abc a1 a2 a3 all a12 ba ba.1 ba.2 filex filey AbC ABC ABc2 abc |
2: | Write and test the command that will do the following:
|
1: | What are the names of the three file streams associated with your terminal? |
2: | What is a file descriptor? |
3: | What command would you use to do the following:
|
4: | What happens when you type cp all by itself? |
5: | How do you save the error message from the above example to a file? |
6: | Use the find command to find all files, starting from the parent directory, of type directory. Save the standard output in a file called found and any errors in a file called found.errs. |
7: | Take the output of three commands and redirect the output to a file called gottem_all? |
8: | Use a pipe(s) with the ps and wc commands to find out how many processes you are currently running. |
1: | What is a positional parameter? Type at the command line: set dogs cats birds fish
|
2: | What is an environment variable? What is the command used to list them? Create an environment variable called CITY and assign it the value of your home town. How do you export it? |
3: | What is a local variable? Set a local variable to your name. Print its value. Unset it. |
4: | What is the function of declare -i? |
5: | What does the $$ variable display? What does the $! display? |
[1] Although bash is traditionally the default shell for Linux platforms, it now comes bundled with Solaris 8.
[2] To get the latest version of bash, visit http://www.delorie.com/gnu/.
[3] There are a number of different initialization files used by bash; they are discussed on the next pages.
[4] If the shell is invoked with the -noprofile option, none of the initialization files are read.
[5] If the set -o (editor) has not been set, but the EDITOR variable has been set to either emacs or vi, then bash will use that definition.
[6] vi is case-sensitive; an uppercase J and a lowercase j are different commands.
[7] Whether the user saves and quits the editor, or simply quits the editor, the commands will all be executed, unless they are commented or deleted.
[8] The tilde character will not be expanded if enclosed in either double or single quotes.
[9] Bash Reference Manual: http://www.delorie.com/gnu/docs/bash/bashref_56.html.
[10] A variable set to a value or to null will be displayed by using the set command, but an unset variable will not.
[11] Like DNA, inheritance goes one direction only, from parent to child.
[12] On bash versions 2.x, printf is a built-in command.
[13] The bash shell allows backquotes for command substitution for upward-compatibility, but provides an alternate method as well.
CONTENTS |