- 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
Special Characters
Some characters have special meaning to Linux shells, so using them as part of variable names or strings causes your program to behave incorrectly. If a string contains such characters, you also have to use escape characters (backslashes) to indicate that the special characters should not be treated as special characters. Some of these characters are shown in Table 25.2.
Table 25.2. Special Shell Characters
| Character | Explanation |
| $ | Indicates the beginning of a shell variable name |
| | | Pipes standard output to next command |
| # | Starts a comment |
| & | Executes a process in the background |
| ? | Matches one character |
| * | Matches one or more characters |
| > | Output redirection operator |
| < | Input redirection operator |
| ` | Command substitution (the backquote or backtick—the key above the Tab key on most keyboards) |
| >> | Output redirection operator (to append to a file) |
| << | Wait until following end-of-input string (HERE operator) |
| [ ] | Lists a range of characters |
| [a-z] | All characters a through z |
| [a,z] | Characters a or z |
| . filename | Executes ("sources") the file filename |
| Space | Delimiter between two words |
A few characters deserve special note. They are the double quotes ("), the single quotes ('), the backslash (\), and the backtick (`), all discussed in the following sections. Also note that you can use input and output redirection from inside your shell scripts. Be sure to use output redirection with care when you're testing your shell programs, because you can easily overwrite files!
Double Quotes
If a string contains embedded spaces, you can enclose the string in double quotes (") so the shell interprets the whole string as one entity instead of more than one. For example, if you assigned the value of abc def (abc followed by one space followed by def) to a variable called x in a shell program as follows, you would get an error because the shell would try to execute def as a separate command.
| Command | Environment |
| x=abc def | pdksh and bash |
| set x = abc def | tcsh |
What you need to do is surround the string in double quotes:
| Command | Environment |
| x="abc def" | pdksh and bash |
| set x = "abc def" | tcsh |
The double quotes resolve all variables within the string. Here is an example for pdksh and bash:
var="test string" newvar="Value of var is $var" echo $newvar
Here is the same example for tcsh:
set var = "test string" set newvar = "Value of var is $var" echo $newvar
If you execute a shell program containing these three lines, you get the following result:
Value of var is test string
Single Quotes
You can surround a string with single quotes (') to stop the shell from resolving a variable. In the following examples, the double quotes in the preceding examples have been changed to single quotes.
pdksh and bash: var='test string' newvar='Value of var is $var' echo $newvar tcsh: set var = 'test string' set newvar = 'Value of var is $var' echo $newvar
If you execute a shell program containing these three lines, you get the following result:
Value of var is $var
As you can see, the variable var did not get interpolated.
Backslash
You can use a backslash (\) before a character to stop the shell from interpreting the succeeding character as a special character. Say you want to assign a value of $test to a variable called var. If you use the following command, a null value is stored in var:
| Command | Environment |
| var=$test | pdksh and bash |
| set var = $test | tcsh |
This happens because the shell interprets $test as the value of the variable test. No value has been assigned to test, so var contains null. You should use the following command to correctly store $test in var:
| Command | Environment |
| var=\$test | pdksh and bash |
| set var = \$test | tcsh |
The backslash (\) before the dollar sign ($) signals the shell to interpret the $ as any other ordinary character and not to associate any special meaning to it.
Backtick
You can use the backtick (`) character to signal the shell to execute the string delimited by the backtick. This can be used in shell programs when you want the result of execution of a command to be stored in a variable. For example, if you want to count the number of lines in a file called test.txt in the current directory and store the result in a variable called var, you can use the following command:
| Command | Environment |
| var=`wc -l test.txt` | pdksh and bash |
| set var = `wc -l test.txt` | tcsh |
Comparison of Expressions | Next Section

Account Sign In
View your cart