An Introduction to Tiger Terminal, Part 4
Pages: 1, 2, 3, 4, 5
Variables
So far, we've created shell scripts using some of the simple Unix commands that we've learned from previous parts of this tutorial. Every shell program has a scripting language built in, and has special commands that are part of its own scripting language. The standard language for writing Unix scripts is the language of the Bourne shell. Most shells (sh, bash, ksh, and zsh) will run it. However, csh and tcsh run a different scripting language from these others. When OS X 10.3 (Panther) was released, with its new default bash shell, it caused a lot of grumbling among scripters who were comfortable using the default tcsh shell in Mac OS X 10.2 (Jaguar), because many commands have different syntax in bash.
The bash shell has a wide range of programming features, including user-defined functions, conditional statements (if/then), loops (for and while), mathematical operations, and variables. A variable is simply a name associated with a value: a string that represents data. Within bash, there are standard variables defined by the shell itself. Let's use two of them in our next shell script.
1. Open a new Terminal window, navigate to /Documents/scripts, and open nano, naming a new script, thirdscript.sh.

Figure 22. nano thirdscript.sh
2. Create the following script, using your own comments, and tweak the text a bit to show your own flair!

Figure 23. thirdscript.sh
3. Save the script (^O) and exit (^X).
4. Don't forget to change permissions!
norburym15:~/documents/scripts norburym$ chmod +x thirdscript.sh
5. Run it!

Figure 24. The output of thirdscript.sh
Both $USER and $PWD are standard bash variables.
Here is another shell script, using the $HOME variable:
1. Open a new Terminal window, navigate to /Documents/scripts, and open nano naming a new script, fourthscript.sh.
2. Type in the following script:

Figure 25. fourthscript.sh
3. Save the script (^O), exit (^X), and change permissions with chmod +x fourthscript.sh.
4. Run this script:

Figure 26. The output of fourthscript.sh
You can also set your own variables. Here's a classic (and, sorry, woefully overused) example:
1. Still in your scripts folder, open nano and name a new script overused.sh.

Figure 27. nano overused.sh
2. Type the following script, save it (^O), and exit (^X) nano:

Figure 28. overused.sh
3. Permissions magic:
norburym15:~/documents/scripts norburym$ chmod +x overused.sh
4. Run it!

Figure 29. The output of overused.sh
Here, we've created our own variables, MY_GREETING and YOUR_RETORT, and assigned each of them a unique value: the string "Hello World!" and the string "Oh, go jump in the lake!" respectively. The equal sign (=) is called the assignment operator. There are no spaces on either side of the = symbol.
Go Get 'Em, Tiger!
We've obviously only scratched the surface here, but I hope these examples have given you ideas about how to harness the power of the Terminal by creating your own shell scripts! There are some really good books out there to help speed you on your way:
- Wicked Cool Shell Scripts, by Dave Taylor (No Starch Press)
- Learning the bash Shell, 3rd Edition, by Cameron Newham and Bill Rosenblatt (O'Reilly)
- Classic Shell Scripting, by Arnold Robbins and Nelson H.F. Beebe (O'Reilly)
Mary Norbury-Glaser is the IT director at a University of Colorado affiliate center. She has over 15 years of experience in cross-platform systems administration in the education sector. She loves fast cars and geocaching.
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 4 of 4.
-
.bash_profile...
2005-09-18 13:12:55 werkflow [Reply | View]
-
.bash_profile...
2005-09-18 18:25:56 norburym [Reply | View]
1. The .bash_profile might not exist if you haven't been following this series from part one (where we created the file) or if you have not used the terminal before. If it wasn't there when you started, it's nothing to worry about.
2. From the command line, type:
$cd /etc/
$ls
You should see a file in there called profile and another file called bashrc. Open profile with nano.
$nano profile
You should see something like this:
# System-wide .profile for sh(1)
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
The info in this file states the default bash PATH. It then instructs bash to read the /etc/bashrc file. Next, bash reads the following, in order:
~/.bash_profile
~/.bash_login
~/.profile
If it finds the first file, it will stop. If it doesn't find that one, it will continue on down the list.






1. .bash_profile never exists in my home. I did not delete it.
2. When I create .bash_profile and type "echo $PATH" in the Terminal.app, I only geht my new ~/Docuements/scripts-Directory. Even no binarys will work without "/bin/...".
Any idea?