HOSTNAME="/bin/hostname"
HISTSIZE=1000
if[-z"$INPUTRC"-a!-f"$HOME/.inputrc"];then INPUTRC=/etc/inputrc fi
exportPATHUSERLOGNAMEMAILHOSTNAMEHISTSIZEINPUTRC
foriin/etc/profile.d/*.sh;do if[-r"$i"];then .$i fi done
unseti unsetpathmunge Example13-2 shows a system-wide shows a system-wide .bashrc .bashrc file. This file is not executed by default when file. This file is not executed by default when bash bash starts. Instead, it is optionally executed by users" local starts. Instead, it is optionally executed by users" local .bashrc .bashrc files. files.
Example13-2.An example system-wide .bashrc file #/etc/bashrc #Systemwidefunctionsandaliases #Environmentstuffgoesin/etc/profile
#Bydefault,wewantthistogetset.
#Evenfornon-interactive,non-loginsh.e.l.ls.
if[$UID-gt99]&&[""id-gn""=""id-un""];then umask002 else umask022 fi
#areweaninteractivesh.e.l.l?
if["$PS1"];then case$TERMin xterm*) if[-e/etc/sysconfig/bash-prompt-xterm];then PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm else PROMPT_COMMAND="echo-ne" 33]0;${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}";echo-ne" 07""
fi ;; screen) if[-e/etc/sysconfig/bash-prompt-screen];then PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen else PROMPT_COMMAND="echo-ne" 33_${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}";echo-ne" 33""
fi ;; *) [-e/etc/sysconfig/bash-prompt-default]&&PROMPT_COMMAND= /etc/sysconfig/bash-prompt-default ;; esac #Turnoncheckwinsize shopt-scheckwinsize ["$PS1"="s-v$"]&&PS1="[[email protected]]$"
fi
if!shopt-qlogin_sh.e.l.l;then#We"renotaloginsh.e.l.l #Needtoredefinepathmunge,itgetsundefinedattheendof/etc/profile pathmunge(){ if!echo$PATH|/bin/egrep-q"(^|:)$1($|:)";then if["$2"="after"];then PATH=$PATH:$1 else PATH=$1:$PATH fi fi }
foriin/etc/profile.d/*.sh;do if[-r"$i"];then .$i fi done unseti unsetpathmunge fi #vim:ts=4:sw=4 aliasmore="less"#preferthe"less"pager aliaslsps="ls-l;ps"#adubiouscommand Example13-3 shows an example user"s local shows an example user"s local .bash_profile .bash_profile. Note that this file sources the user"s .bashrc .bashrc file (which in turn sources the system-wide file (which in turn sources the system-wide /etc/bashrc /etc/bashrc), and then goes on to local customizations.
Example13-3.An example user"s .bash_profile file #.bash_profile #Getthealiasesandfunctions if[-f~/.bashrc];then .~/.bashrc fi
#Userspecificenvironmentandstartupprograms PATH=$PATH:$HOME/bin exportPATH unsetUSERNAME Example13-4 shows an individual"s shows an individual"s .bashrc .bashrc file. file.
Example13-4.An example user"s .bashrc file #.bashrc
#Userspecificaliasesandfunctions
aliasrm="rm-i"
aliascp="cp-i"
aliasmv="mv-i"
#Sourceglobaldefinitions if[-f/etc/bashrc];then ./etc/bashrc fi lsps(){#Defineapersonalfunction ls-l$1 psaux|grep"/bin/basename$1"
} Example13-5 shows a short, simple, and not uncommon shows a short, simple, and not uncommon .bash_logout .bash_logout file. Probably the most likely command to find in a logout file is the file. Probably the most likely command to find in a logout file is the clear clear command. Including a command. Including a clear clear in your logout file is a nice way of being certain that whatever you were doing just before you log out won"t linger on the screen for the next user to ponder. This file is intended to execute commands for a logout from a text session, such as a system console or terminal. In a GUI environment where logout and login are handled by a GUI program, in your logout file is a nice way of being certain that whatever you were doing just before you log out won"t linger on the screen for the next user to ponder. This file is intended to execute commands for a logout from a text session, such as a system console or terminal. In a GUI environment where logout and login are handled by a GUI program, .bash_logout .bash_logout might not be of much value. might not be of much value.
Example13-5.A simple .bash_logout file #.bash_logout #Thisfileis.e.xecutedwhenauserlogsoutofthesystem /usr/bin/clear#Clearthescreen /usr/games/fortune#PrintarandomadageOn the ExamMake certain that you understand the difference between execution at login and execution at sh.e.l.l invocation, as well as which of the startup files serves each of those purposes.
.inputrc Among the many enhancements added to bash bash is the ability to perform as if your history of commands is the buffer of an editor. That is, your command history is available to you, and you may cut, paste, and even search among command lines entered previously. This powerful capability can significantly reduce typing and increase accuracy. By default, is the ability to perform as if your history of commands is the buffer of an editor. That is, your command history is available to you, and you may cut, paste, and even search among command lines entered previously. This powerful capability can significantly reduce typing and increase accuracy. By default, bash bash is configured to emulate the Emacs editor, but a is configured to emulate the Emacs editor, but a vi vi editing interface is also available. editing interface is also available.
The portion of bash bash that handles this function, and in fact handles all of the line input during interactive use, is known as that handles this function, and in fact handles all of the line input during interactive use, is known as readline readline. Readline may be customized by putting commands into an initialization file, which by default is in your home directory and called .inputrc .inputrc. For example, to configure bash bash to use to use vi vi-style editing keys, add this line to .inputrc .inputrc: setediting-modeviNoteYou may also set the INPUTRC INPUTRC variable to the name of another file if you prefer. On your system, this variable may be set to /etc/initrc by default, which would override any settings you put into a local variable to the name of another file if you prefer. On your system, this variable may be set to /etc/initrc by default, which would override any settings you put into a local .initrc .initrc. To use your own file, you must first explicitly place the command unset INPUTRC unset INPUTRC in your in your .bash_profile .bash_profile.
The default editing facilities enabled in bash bash are extensive and are beyond the scope of this section and Exam 102. However, you need to understand the concepts of adding your own custom key bindings to the are extensive and are beyond the scope of this section and Exam 102. However, you need to understand the concepts of adding your own custom key bindings to the .inputrc .inputrc file and how they can help automate common keystrokes unique to your daily routine for the test. file and how they can help automate common keystrokes unique to your daily routine for the test.
For example, suppose you often use top top to watch your system"s activity ( to watch your system"s activity (top is a useful process-monitoring utility that is described in is a useful process-monitoring utility that is described in Chapter6 Chapter6): $top-Ssd1 If you do this often enough, you"ll get tired of typing the command over and over and will eventually want an alias for it. To create the alias, simply alias this command to top top: $aliastop="/usr/bin/top-Ssd1"
Better yet, you can use .inputrc .inputrc to create a key binding that will enter it for you. Here"s how the to create a key binding that will enter it for you. Here"s how the .inputrc .inputrc file would look if you were to bind your file would look if you were to bind your top top command to the key sequence Ctrl-T: command to the key sequence Ctrl-T: #my.inputrcfile Control-t:"top-Ssd1C-m"
The lefthand side of the second line indicates the key combination you wish to use (Ctrl-T). The righthand side indicates what you wish to bind to that key sequence. In this case, bash bash outputs outputs top -Ssd1 top -Ssd1 and a carriage return, denoted here by and a carriage return, denoted here by C-m C-m (Ctrl-M), when Control-T is pressed. (Ctrl-M), when Control-T is pressed.
Through modifications of your local configuration files, you can customize your environment and automate many of your daily tasks. You may also override system-wide settings in your personal files simply by setting variables, aliases, and functions.
On the ExamYou won"t need to have detailed knowledge of this key-binding syntax, but be aware of the .inputrc .inputrc file and the kinds of things it enables file and the kinds of things it enables bash bash to do. to do.
Objective 2: Customize or Write Simple Scripts You"ve seen how the use of bash bash configuration files, aliases, functions, variables, and key bindings can customize and make interaction with your Linux system efficient. The next step in your relationship with the sh.e.l.l is to use its natural programming capability, or configuration files, aliases, functions, variables, and key bindings can customize and make interaction with your Linux system efficient. The next step in your relationship with the sh.e.l.l is to use its natural programming capability, or scripting language scripting language. The scripting language of the original Bourne sh.e.l.l is found throughout a Linux system, and bash bash is fully compatible with it. This section covers essential is fully compatible with it. This section covers essential bash bash scripting language concepts as required for Exam 102. scripting language concepts as required for Exam 102.
In order to have a full appreciation of sh.e.l.l scripting on Linux, it"s important to look at your Linux system as a collection of unique and powerful tools. Each of the commands available on your Linux system, along with those you create yourself, has some special capability, and by combining them, you are able to have a productive and maintainable environment.
Script Files Just as the configuration files discussed in the last section are plain text files, so are the scripts for your sh.e.l.l. In addition, unlike compiled languages such as C or Pascal, no compilation of a sh.e.l.l program is necessary before it is executed. You can use any editor to create script files, and you"ll find that many scripts you write are portable from Linux to other Unix systems.
Creating a simple bash script The simplest scripts are those that string together some basic commands and perhaps do something useful with the output. Of course, this can be done with a simple alias or function, but eventually you"ll have a requirement that exceeds a one-line request, and a sh.e.l.l script is the natural solution. Aliases and functions have already been used to create a rudimentary new command, lsps lsps. Now let"s look at a sh.e.l.l script (Example13-6) that accomplishes the same thing.
Example13-6.The lsps script #Abasiclspscommandscriptforbash ls-l$1 ps-aux|grep"/bin/basename$1"
As you can see, the commands used in this simple script are identical to those used in the alias and in the function created earlier. To make use of this new file, instruct your currently running bash bash sh.e.l.l to source it, giving it an option for the sh.e.l.l to source it, giving it an option for the $1 $1 positional parameter: positional parameter: $source./lsps/usr/sbin/httpd If you have /usr/sbin/httpd /usr/sbin/httpd running, you should receive output similar to that found previously for the alias. By replacing the word running, you should receive output similar to that found previously for the alias. By replacing the word source source with a single dot, you can create an alternate shorthand notation to tell with a single dot, you can create an alternate shorthand notation to tell bash bash to source a file, as follows: to source a file, as follows: $../lsps/usr/sbin/httpd Another way to invoke a script is to start a new invocation of bash bash and tell that process to source the file. To do this, simply start and tell that process to source the file. To do this, simply start bash bash and pa.s.s the script name and argument to it: and pa.s.s the script name and argument to it: $/bin/bash./lsps/usr/sbin/httpd This last example gives us the same result; however, it is significantly different from the alias, the function, or the sourcing of the lsps lsps file. In this particular case, a new invocation of file. In this particular case, a new invocation of bash bash was started to execute the commands in the script. This is important, because the environment in which the commands are running is distinct from the environment in which the user is typing. This is described in more detail later in this section. was started to execute the commands in the script. This is important, because the environment in which the commands are running is distinct from the environment in which the user is typing. This is described in more detail later in this section.
NoteThe ./ ./ syntax indicates that the file you"re referring to is in the current working directory. For security reasons, it is not advisable to add syntax indicates that the file you"re referring to is in the current working directory. For security reasons, it is not advisable to add . . to a user"s to a user"s $PATH $PATH variable. Instead, either type the relative path to the command ( variable. Instead, either type the relative path to the command (./lsps) or the full path (/usr/bin/lsps).
Thus far, a sh.e.l.l script has been created and invoked in a variety of ways, but it hasn"t been made into a command. A script really becomes useful when it can be called by name like any other command.
Executable files On a Linux system, programs are said to be executable if they have content that can be run by the processor (native execution) or by another program such as a sh.e.l.l (interpreted execution). However, in order to be eligible for execution when called at the command line, the files must have attributes that indicate to the sh.e.l.l that they are executable. To make a file executable, it must have at least one of its executable bits executable bits set. To turn the example script from a plain text file into an executable program, that bit must be set using the set. To turn the example script from a plain text file into an executable program, that bit must be set using the chmod chmod command: command: $chmoda+xlsps More information on chmod chmod can be found in can be found in Chapter7 Chapter7.
Once this is done, the script is executable by its owner, group members, and everyone else on the system. At this point, running the new command from the bash bash prompt yields the familiar output: prompt yields the familiar output: $./lsps/usr/sbin/httpd When lsps lsps is called by name, the commands in the script are interpreted and executed by the is called by name, the commands in the script are interpreted and executed by the bash bash sh.e.l.l. However, this isn"t ultimately what is desired. In many cases, users will be running some other sh.e.l.l interactively but will still want to program in sh.e.l.l. However, this isn"t ultimately what is desired. In many cases, users will be running some other sh.e.l.l interactively but will still want to program in bash bash. Programmers also use other scripting languages such as Perl or Python. To have the scripts interpreted correctly, the system must be told which program should interpret the commands in the scripts.
Shebang!
Many kinds of script files are found on a Linux system, and each interpreted language comes with a unique and specific command structure. There needs to be a way to tell Linux which interpreter to use for each script. This is accomplished by using a special line at the top of the script naming the appropriate interpreter. Linux examines this line and launches the specified interpreter program, which then reads the rest of the file. The special line must begin with #! #!, a construct often called shebang shebang, often thought of as being short for Sh Sharp (#) Bang Bang (!). For (!). For bash bash, the shebang line is: #!/bin/bash This command explicitly states that the program named bash bash can be found in the can be found in the /bin /bin directory and designates directory and designates bash bash to be the interpreter for the script. You"ll also see other types of lines on script files, including: to be the interpreter for the script. You"ll also see other types of lines on script files, including: #!/bin/sh The Bourne sh.e.l.l #!/bin/csh The C-sh.e.l.l #!/bin/tcsh The enhanced C-sh.e.l.l #!/bin/sed The stream editor #!/usr/bin/awk The awk programming language #!/usr/bin/perl The Perl programming language Each of these lines specifies a unique command interpreter for the script lines that follow. (bash is fully backward-compatible with is fully backward-compatible with sh sh; sh sh is just a link to is just a link to bash bash on Linux systems.). Note that the full paths given here are the default; some distributions might have slight differences. For example, Perl is often in on Linux systems.). Note that the full paths given here are the default; some distributions might have slight differences. For example, Perl is often in /bin/perl /bin/perl or even or even /usr/local/bin/perl /usr/local/bin/perl.
On the ExamAn incorrectly stated shebang line can cause the wrong interpreter to attempt to execute commands in a script.
The sh.e.l.l script"s environment When running a script with #!/bin/bash #!/bin/bash, a new invocation of bash bash with its own environment is started to execute the script"s commands as the parent sh.e.l.l waits. Exported variables in the parent sh.e.l.l are copied into the child"s environment; the child sh.e.l.l executes the appropriate sh.e.l.l configuration files (such as with its own environment is started to execute the script"s commands as the parent sh.e.l.l waits. Exported variables in the parent sh.e.l.l are copied into the child"s environment; the child sh.e.l.l executes the appropriate sh.e.l.l configuration files (such as .bash_profile .bash_profile). Because configuration files will be run, additional sh.e.l.l variables may be set and environment variables may be overwritten. If you are depending upon a variable in your sh.e.l.l script, be sure that it is either set by the sh.e.l.l configuration files or exported into the environment for your use, but not both.
Another important concept regarding your sh.e.l.l"s environment is known as unidirectional unidirectional or or one-way inheritance one-way inheritance. Although your current sh.e.l.l"s environment is pa.s.sed into into a sh.e.l.l script, that environment is a sh.e.l.l script, that environment is not pa.s.sed back not pa.s.sed back to the original sh.e.l.l when your program terminates. This means that changes made to variables during the execution of your script are not preserved when the script exits. Instead, the values in the parent sh.e.l.l"s variables are the same as they were before the script executed. This is a basic Unix construct; inheritance goes from parent process to child process, and not the other way around. to the original sh.e.l.l when your program terminates. This means that changes made to variables during the execution of your script are not preserved when the script exits. Instead, the values in the parent sh.e.l.l"s variables are the same as they were before the script executed. This is a basic Unix construct; inheritance goes from parent process to child process, and not the other way around.
On the ExamIt is important to remember how variables are set, how they are inherited, and that they are inherited only from parent process to child process.
Location, ownership, and permissions The ability to run any executable program, including a script, under Linux depends in part upon its location in the filesystem. Either the user must explicitly specify the location of the file to run or it must be located in a directory known by the sh.e.l.l to contain executables. Such directories are listed in the PATH PATH environment variable. For example, the sh.e.l.ls on a Linux system (including environment variable. For example, the sh.e.l.ls on a Linux system (including bash bash) are located in /bin /bin. This directory is usually in the PATH PATH, because you"re likely to run programs that are stored there. When you create sh.e.l.l programs or other utilities of your own, you may want to keep them together and add the location to your own PATH PATH. If you maintain your own bin bin directory, you might add the following line to your directory, you might add the following line to your .bash_profile .bash_profile: PATH=$PATH:$HOME/bin This statement modifies your path to include your /home/username/bin /home/username/bin directory. If you add personal scripts and programs to this directory, directory. If you add personal scripts and programs to this directory, bash bash finds them automatically. finds them automatically.
Execute permissions (covered in the section Objective 5: Manage File Permissions and Ownership Objective 5: Manage File Permissions and Ownership) also affect your ability to run a script. Since scripts are just text files, execute permission must be granted to them before they are considered executable, as shown earlier.
You may wish to limit access to the file from other users with the following: $chmod700~/bin/lsps This prevents anyone but the owner from making changes to the script.
The issue of file ownership is dovetailed with making a script executable. By default, you own all of the files you create. However, if you are the system administrator, you"ll often be working as the superuser and will be creating files with username root root as well. It is important to a.s.sign the correct ownership and permission to scripts to ensure that they are secured. as well. It is important to a.s.sign the correct ownership and permission to scripts to ensure that they are secured.
SUID and SGID rights On rare occasions, it may become necessary to allow a user to run a program under the name of a different user. This is usually a.s.sociated with programs run by nonprivileged users that need special privileges to execute correctly. Linux offers two such rights: SUID and SGID.
When an executable file is granted the SUID right, processes created to execute it are owned by the user who owns the file instead of the user who launched the program. This is a security enhancement, in that the delegation of a privileged task or ability does not imply that the superuser pa.s.sword must be widely known. On the other hand, any process whose file is owned by root root and that has the SUID set will run as and that has the SUID set will run as root root for everyone. This could represent an opportunity to break the security of a system if the file itself is easy to attack (as a script is). For this reason, Linux systems will ignore SUID and SGID attributes for script files. Setting SUID and SGID attributes is detailed in for everyone. This could represent an opportunity to break the security of a system if the file itself is easy to attack (as a script is). For this reason, Linux systems will ignore SUID and SGID attributes for script files. Setting SUID and SGID attributes is detailed in Objective 5: Manage File Permissions and Ownership Objective 5: Manage File Permissions and Ownership.
On the ExamBe sure to think through any questions that require you to determine a user"s right to execute a file. Consider location, ownership, execute permissions, and SUID/SGID rights together.
Basic Bash Scripts Now that some of the requirements for creating and using executable scripts are established, some of the features that make them so powerful can be introduced. This section contains basic information needed to customize and create new bash bash scripts. scripts.
Return values As sh.e.l.l scripts execute, it is important to confirm that their const.i.tuent commands complete successfully. Most commands offer a return value return value to the sh.e.l.l when they terminate. This value is a simple integer and has a meaning specific to the program you"re using. Almost all programs return the value 0 when they are successful and return a nonzero value when a problem is encountered. The value is stored in the special to the sh.e.l.l when they terminate. This value is a simple integer and has a meaning specific to the program you"re using. Almost all programs return the value 0 when they are successful and return a nonzero value when a problem is encountered. The value is stored in the special bash bash variable variable $? $?, which can be tested in your scripts to check for successful command execution. This variable is reset for every command executed by the sh.e.l.l, so you must test it immediately after execution of the command you"re verifying. As a simple example, try using the cat cat program on a nonexistent file: program on a nonexistent file: $catbogus_file cat:bogus_file:Nosuchfileordirectory Then immediately examine the status variable twice: $echo$?
1 $echo$?
0 The first echo echo yielded yielded 1 1 (failure) because the (failure) because the cat cat program failed to find the file you specified. The second program failed to find the file you specified. The second echo echo yielded yielded 0 0 (success) because the first (success) because the first echo echo command succeeded. A good script makes use of these status flags to exit gracefully in case of errors. command succeeded. A good script makes use of these status flags to exit gracefully in case of errors.
If it sounds backward to equate zero with success and nonzero with failure, consider how these results are used in practice: Error detection Scripts that check for errors include if-then if-then code to evaluate a command"s return status: code to evaluate a command"s return status:command if(failure_returned);then ...errorrecoverycode...
fiIn a bash bash script, script, failure_returned failure_returned is examining the value of the is examining the value of the $? $? variable, which contains the result of the command"s execution. variable, which contains the result of the command"s execution.
Error cla.s.sification Since commands can fail for multiple reasons, many return more than one failure code. For example, grep grep returns returns 0 0 if matches are found and if matches are found and 1 1 if no matches are found; it returns if no matches are found; it returns 2 2 if there is a problem with the search pattern or input files. Scripts may need to respond differently to various error conditions. if there is a problem with the search pattern or input files. Scripts may need to respond differently to various error conditions.On the ExamMake certain you understand the meaning of return values in general and that they are stored in the $? $? variable. variable.
File tests During the execution of a sh.e.l.l script, specific information about a file-such as whether it exists, is writable, is a directory or a file, and so on-may sometimes be required. In bash bash, the built-in command test test performs this function. (There is also a standalone executable version of performs this function. (There is also a standalone executable version of test test available in available in /usr/bin /usr/bin for non-bash sh.e.l.ls.) for non-bash sh.e.l.ls.) test test has two general forms: has two general forms: test expression expression In this form, test test and an and an expression expression are explicitly stated. are explicitly stated.
[ expression expression ] ]
In this form, test test isn"t mentioned; instead, the isn"t mentioned; instead, the expression expression is enclosed inside brackets. is enclosed inside brackets.
The expression expression can be formed to look for such things as empty files, the existence of files, the existence of directories, equality of strings, and others. (See the more complete list with their operators in section, can be formed to look for such things as empty files, the existence of files, the existence of directories, equality of strings, and others. (See the more complete list with their operators in section, Abbreviated bash command reference Abbreviated bash command reference. The bash manpage also details all the test test options that are available.) options that are available.) When used in a script"s if if or or while while statement, the brackets ( statement, the brackets ([ and and ] ]) may appear to be grouping the test logically. In reality, [ [ is simply another form of the is simply another form of the test test command, which requires the trailing command, which requires the trailing ] ]. A side effect of this bit of trickery is that the s.p.a.ces around [ [ and and ] ] are mandatory, a detail that is sure to get you into trouble eventually. are mandatory, a detail that is sure to get you into trouble eventually.
Command subst.i.tution bash offers a handy ability to do offers a handy ability to do command subst.i.tution command subst.i.tution with the with the $(command) $(command) or or "command" "command" syntax. Wherever syntax. Wherever $( $(command) is found, its output is subst.i.tuted prior to interpretation by the sh.e.l.l. For example, to set a variable to the number of lines in your is found, its output is subst.i.tuted prior to interpretation by the sh.e.l.l. For example, to set a variable to the number of lines in your .bashrc .bashrc file, you could use file, you could use wc -l wc -l: $RCSIZE=$(wc-l~/.bashrc) Another form of command subst.i.tution encloses command command in in backquotes backquotes or backticks ( or backticks ("): $RCSIZE="wc-l~/.bashrc"
The result is the same, except that the backquote syntax allows the backslash character to escape the dollar symbol ($), the backquote ("), and another backslash (). The $( $(command) syntax avoids this nuance by treating all characters between the parentheses literally. syntax avoids this nuance by treating all characters between the parentheses literally.
Mailing from scripts The scripts you write will often be rummaging around your system at night when you"re asleep or at least while you"re not watching. Since you"re too busy to check on every script"s progress, a script will sometimes need to send some mail to you or another administrator. This is particularly important when something big goes wrong or when something important depends on the script"s outcome. Sending mail is as simple as piping into the mail mail command: command: echo"Backupfailure5"|mail-s"Backupfailed"root The -s -s option indicates that a quoted subject for the email follows. The recipient could be yourself, option indicates that a quoted subject for the email follows. The recipient could be yourself, root root, or if your system is configured correctly, any Internet email address. If you need to send a logfile, redirect the input of mail mail from that file: from that file: mail-s"subject"recipient
Abbreviated bash command reference This section lists some of the important bash bash built-in commands used when writing scripts. Please note that not all of the built-in commands used when writing scripts. Please note that not all of the bash bash commands are listed here; for a complete overview of the commands are listed here; for a complete overview of the bash bash sh.e.l.l, see sh.e.l.l, see Learning the bash Sh.e.l.l by Cameron Newham (O"Reilly). by Cameron Newham (O"Reilly).
Name break Syntax break[n]
Description Exit from the innermost (most deeply nested) for for, while while, or until until loop or from the loop or from the n n innermost levels of the loop. innermost levels of the loop.
Name case Syntax casestringin pattern1) commands1 ;; pattern2) commands2 ;; ...
esac Description Choose string string from among a series of possible patterns. These patterns use the same form as file globs (wildcards). If from among a series of possible patterns. These patterns use the same form as file globs (wildcards). If string string matches pattern matches pattern pattern1 pattern1, perform the subsequent commands1 commands1. If string matches pattern2 pattern2, perform commands2 commands2. Proceed down the list of patterns until one is found. To catch all remaining strings, use *) *) at the end. at the end.
Name continue Syntax continue[n]
Description Skip remaining commands in a for for, while while, or until until loop, resuming with the next iteration of the loop (or skipping loop, resuming with the next iteration of the loop (or skipping n n loops). loops).
Name echo Syntax echo[options][string]
Description Write string string to standard output, terminated by a newline. If no string is supplied, echo only a newline. Some Linux distributions have a version of to standard output, terminated by a newline. If no string is supplied, echo only a newline. Some Linux distributions have a version of echo echo at at /bin/echo /bin/echo. If that is the case, the built-in bash bash version of version of echo echo will usually take precedence. will usually take precedence.
Frequently used options -e Enable interpretation of escape characters.
-n Suppress the trailing newline in the output.