What follows is a very basic shell script that prints “Hello, world!” to the screen:
#! /bin/sh |
echo "Hello, world!" |
The first thing you should notice is that the script starts with ‘#!’. This is known as an interpreter line. If you don’t specify an interpreter line, the default is usually the Bourne shell (/bin/sh). However, it is best to specify this line anyway for consistency.
The second thing you should notice is the echo statement. This is nearly universal in shell scripting as a means for printing something to the user’s screen.
If you’d like, you can try this script by saving those lines in a text file (say “hello_world.sh”) in your home directory. Then, in Terminal, type:
chmod u+x hello_world.sh |
./hello_world.sh |
Of course, this script isn’t particularly useful. It just prints the words “Hello, world!“ to your screen. To make this more interesting, the next script throws in a few variables.
#! /bin/sh |
FIRST_ARGUMENT="$1" |
echo "Hello, world $FIRST_ARGUMENT!" |
Type or paste this script into the text editor of your choice (TextEdit, for example) and save the file in your home directory in a file called test.sh. Then type ‘chmod a+x test.sh’ in Terminal to make it executable. Finally, run it with ‘./test.sh leaders’. You should see “Hello, world leaders!” printed to your screen.
This script provides an example of a variable assignment. The variable $1 contains the first argument passed to the shell script. In this example, the script makes a copy and stores it into a variable called FIRST_ARGUMENT, then prints that variable.
You should immediately notice that, depending on use, variables may or may not begin with a dollar sign. If you are dereferencing the variable, you precede it with a dollar sign. The shell inserts the contents of the variable at that point in the script. You would not want to do this on the left side of the assignment statement, however, since FIRST_ARGUMENT starts out empty, and the first line would then evaluate to the following:
="myfirstcommandlineargument" |
This is clearly not what you want.
You should also notice the double quotation marks. These are important, as any command-line argument could potentially contain spaces. Normally, when executing a command, spaces are treated as separating multiple arguments, and thus, shell scripts can behave differently when variables containing spaces are involved. By enclosing the variables in double quotes, they are treated as part of a single argument even if the value stored in the variable contains a space.
Had you omitted the quotes, if the first argument contained spaces (the phrase “This is a test”, for example), the statement might evaluate to something like this:
FIRST_ARGUMENT=This is a test |
The result would be that the shell would try to execute a command called is, passing it two arguments, a and test, and would set the variable FIRST_ARGUMENT to This in the environment it passes to that command. Again, while this behavior is useful, it is clearly not what you intended to do. (This behavior and its uses are described more in “Overriding Environment Variables for Child Processes (Bourne Shell).”)
Note: Expansion of variables, occurs after the statement itself is fully parsed by the shell. Thus, as long as the variable is enclosed in double quote marks, you will not get any execution errors even if the variable’s value contains double-quote marks.
However, if you are using double quote marks within a literal string, you must quote that string properly. For example:
MYSTRING="The word of the day is \"sedentary\"." |
Shell scripts also allow the use of single quote marks. Variables between single quotes are not replaced by their contents. Be sure to use double quotes unless you are intentionally trying to display the actual name of the variable. You can also use single quotes as a way to avoid the shell interpreting the contents of the string in any way. These differences are described further in “Variables, Expansion, and Quoting.”
C Shell Note: The syntax for assignment statements in the C shell is rather different. Instead of an assignment statement, the C shell uses the set and setenv builtins to set variables as shown below:
set VALUE = "Four" |
# or... |
setenv VALUE "Four" |
echo "$VALUE score and seven years ago...." |
set and setenv is described in “Exporting Shell Variables.”Last updated: 2008-04-08