Articles‎ > ‎

Shell Scripting 101: Part 5

posted Dec 19, 2008, 5:48 AM by Philip Rinehart   [ updated Dec 19, 2008, 5:50 AM by Greg Neagle ]
Now it begins to get fun.  We've talked about functions, conditionals, and basic shell scripting syntax.  In this part of shell scripting 101, we'll begin to look at built-in functions of bash.

What is a built-in command?

Bash understands certain commands which are executed by the bash binary.  These commands are not executed by an external program.  Additionally,  there are two types of variables, a regular built-in and a special built-in.  In general, the distinction between the two is not important when writing shell scripts.  When using a built-in, the shell does not fork the process either, which can be more efficient. Let's cover some of the more important commands.


Why use printf instead of echo?  printf behavior is defined by the POSIX standard, which has the side effect of making scripts very portable.  It is also possible to format output in a consistent way using format specifiers.  That may sound like a mouthful, and it is, so let's look at an example of how to use printf in a script.

# !/bin/bash 
var=42printf "%d\n" "$var"

Note how printf is used.  In a script that returns feedback to the user running the script, it can provide a more readable version of the output.  In the above example, the format specifier for a number is used, %d.  Note the use of the newline character as well, \n.  This makes printf even more useful than echo, as one command can create a readable line.  The above example could not be done with the use of echo.  The other important format specifier that is used is the use of %s, for string formatting.  The format is similar to the above example, printf "%s\n" "$mystring".


Read is another useful built-in command.  It causes a shell script to prompt the user for input, and then usually assigns it to a variable.  Here's a real world example:

# A very simple shell script to run SSH in Mac OS X
printf "Enter the ssh server address:"
read server
echo "Please enter your user id:"
read userid
printf "Connecting to $server..."
ssh -l $userid $server

 Note the simplicity of this shell script.  It reads two values from the user, server and userid.  With these two input values, it then initiates the ssh connection.  Quick and easy.


export is used to modify the environment that the bash shell uses.  Remember the use of export to set the PATHvariable from early parts of this series?  Any environment variable can be reset with the use of export in a shell script.  Let's revisit an important environment variable that is commonly set in a script.  It is considered best practice to set the PATH variable at the beginning of a shell script in the following way:

 export PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin

Doing this at the beginning of the shell script can prevent malicious inclusion of directories by accident or design.  This is the most common use of export in a shell script.  Any environment variable can be reset however, such as $HOME, or $SHELLOPTS.


Hopefully shell scripting is becoming more comfortable.  It is time to talk about the use of exec.  exec is different than executing a command from within the shell script.  It replaces the current process with a specified command, and does not fork off a separate process.  Here is and example of the exec command in use that is worthwhile to include in any shell script.

exec 2>/dev/console

Why is this worthwhile?  All output from the shell script is sent to the console log.  Including this line in a shell script sends stderr to the console for debugging or review purposes.  Neat!  One last note, when using exec be cautious as control is not returned to the shell script.  In practice, any command invoked by exec will cause the shell script to pass control to the new program.  Once the program invoked is finished, the shell script will terminate, and not complete any command after the use of exec.  The only exception to this rule is when file redirection is used, as in the above example.


Hmm, a strange built-in indeed.  Command uses the shell built-in variables after the use of external commands.  As an example, cd (Change working directory) is both a built-in command and an external command.  How does this work in practice?   An example:

command cd "/" 

 When used in a shell script, this line will use the external shell command.  In practice, this command is not used that commonly.

For a complete listing of all the built-in commands, consult the bash manual page.  The above built-ins are commonly used in many shell scripts to accomplish a wide variety of tasks.  In the next part of this series, we'll begin to examine how scripts can be written more securely, including a very basic script prologue.  We'll also delve more into debugging shell scripts.