- Table of Contents
- Copyright
- About the Lead Authors
- About the Contributing Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- I. Red Hat Linux Installation and User Services
- Chapter 1. Introduction to Red Hat Linux
- Chapter 2. Installation of Your Red Hat System
- Chapter 3. LILO and Other Boot Managers
- Chapter 4. Configuring the X Window System, Version 11
- Chapter 5. Window Managers
- Chapter 6. Connecting to the Internet
- Chapter 7. IRC, ICQ, and Chat Clients
- Chapter 8. Using Multimedia and Graphics Clients
- II. Configuring Services
- Chapter 9. System Startup and Shutdown
- Chapter 10. SMTP and Protocols
- Chapter 11. FTP
- Chapter 12. Apache Server
- Chapter 13. Internet News
- Chapter 14. Domain Name Service and Dynamic Host Configuration Protocol
- Chapter 15. NIS: Network Information Service
- Chapter 16. NFS: Network Filesystem
- Chapter 17. Samba
- III. System Administration and Management
- Chapter 18. Linux Filesystems, Disks, and Other Devices
- Chapter 19. Printing with Linux
- Chapter 20. TCP/IP Network Management
- Chapter 21. Linux System Administration
- Chapter 22. Backup and Restore
- Chapter 23. System Security
- IV. Red Hat Development and Productivity
- Chapter 24. Linux C/C++ Programming Tools
- Chapter 25. Shell Scripting
- Chapter 26. Automating Tasks
- Chapter 27. Configuring and Building Kernels
- Chapter 28. Emulators, Tools, and Window Clients
- V. Appendixes
- A. The Linux Documentation Project
- B. Top Linux Commands and Utilities
- C. The GNU General Public License
- D. Red Hat Linux RPM Package Listings
Creating and Executing a Shell Program
Say you want to set up a number of aliases whenever you log on. Instead of typing all of the aliases every time you log on, you can put them in a file by using a text editor, such as vi, and then execute the file.
Here is what is contained in myenv, a sample file created for this purpose (for bash):
#!/bin/sh alias ll='ls -l' alias dir='ls' alias copy='cp'
myenv can be executed in a variety of ways under Linux.
You can make myenv executable by using the chmod command, as follows, and then execute it as you would any other native Linux command:
# chmod +x myenv
This turns on the executable permission of myenv. You need to ensure one more thing before you can execute myenv—it must be in the search path. You can get the search path by executing
# echo $PATH
If the directory where the file myenv is located is not in the current search path, you must add the directory name in the search path.
Now you can execute the file myenv from the command line as if it were a Linux command:
# myenv
A second way to execute myenv under a particular shell, such as pdksh, is as follows:
# pdksh myenv
This invokes a new pdksh shell and passes the filename myenv as a parameter to execute the file.
You can also execute myenv from the command line as follows:
| Command Line | Environment |
| # . myenv | pdksh and bash |
| # source myenv | tcsh |
The dot (.) is a way of telling the shell to execute the file myenv. In this case, you do not have to ensure that the execute permission of the file has been set. Under tcsh, you have to use the source command instead of the dot (.) command.
After you execute the command myenv, you should be able to use dir from the command line to get a list of files under the current directory and ll to get a list of files with various attributes displayed. However, the best way to use the new commands in myenv is to put them into your shell's login or profile file. For Red Hat Linux users, the default shell is bash, so make these commands available for everyone on your system by putting them in the /etc/profile.d directory. Copy in a tcsh version with the file extension .csh and the bash/pdksh version with a .sh file extension.
In some instances, you may need to modify how your shell scripts are executed. For example, the majority of shell scripts use a hash-bang line at the beginning, like this:
#!/bin/sh
One of the reasons for this is to control the type of shell used to run the script (in this case, an sh-incantation of bash). Other shells, such as ksh, may respond differently depending on how they're called from a script (hence the reason for symbolic links to different shells).
You may also find different or new environment variables available to your scripts by using different shells. For example, if you launch csh from the bash command line, you'll find at least several new variables, or variables with slightly occluded definitions, such as
# env ... VENDOR=intel MACHTYPE=i386 HOSTTYPE=i386-linux HOST=thinkpad.home.org
On the other hand, bash may provide these variables, or variables of the same name with a slightly different definition, such as
# env ... HOSTTYPE=i386 HOSTNAME=thinkpad.home.org
Although the behavior of a bang line is not defined by POSIX, variations of its incantation can be helpful when you're writing shell scripts. As described in the wish man page, you can use a shell to help execute programs called within a shell script without needing to hard-code pathnames of programs. This increases shell script portability.
For example, if you want to use the wish command (a windowing tcl interpreter), your first inclination may be to write
#!/usr/local/bin/wish
Although this will work on many other operating systems, the script will fail under Linux. However, if you use
#!/bin/sh exec wish "$@"
the wish command (as a binary or itself a shell script) can be used. There are other advantages to using this approach. See the wish man page for more information.
Variables | Next Section

Account Sign In
View your cart