- 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
Variables
Linux shell programming is a full-fledged programming language and, as such, supports various types of variables. Variables have three major types: environment, built-in, and user.
- Environment variables are part of the system environment, and you do not have to define them. You can use them in your shell program. Some of them, such as PATH, can also be modified within a shell program.
- Built-in variables are provided by the system. Unlike environment variables, you cannot modify them.
- User variables are defined by you when you write a shell script. You can use and modify them at will within the shell program.
A major difference between shell programming and other programming languages is that in shell programming, variables are not typecast. That is, you do not have to specify whether a variable is a number or a string, and so on.
Assigning a Value to a Variable
Say you want to use a variable called lcount to count the number of iterations in a loop within a shell program. You can declare and initialize this variable as follows:
| Command | Environment |
| lcount=0 | pdksh and bash |
| set lcount = 0 | tcsh |
Shell programming languages do not use typed variables, so the same variable can be used to store an integer value one time and a string another time. This is not recommended, however, and you should be careful not to do this.
To store a string in a variable, you can use the following:
| Command | Environment |
| myname=Sanjiv | pdksh and bash |
| set myname = Sanjiv | tcsh |
The preceding can be used if the string does not have embedded spaces. If a string has embedded spaces, you can do the assignment as follows:
| Command | Environment |
| myname='Sanjiv Guha' | pdksh and bash |
| set myname = 'Sanjiv Guha' | tcsh |
Accessing Variable Values
You can access the value of a variable by prefixing the variable name with a $ (dollar sign). That is, if the variable name is var, you can access the variable by using $var.
If you want to assign the value of var to the variable lcount, you can do so as follows:
| Command | Environment |
| lcount=$var | pdksh and bash |
| set lcount = $var | tcsh |
Positional Parameters | Next Section

Account Sign In
View your cart