-->
Previous | Table of Contents | Next |
When you create shell variables or give existing variables values, they exist in the running shell. A variable set in the login shell is available to all command-line arguments. A variable set within a shell has that value only within that shell. The value disappears or is reset when you exit that shell.
For example, enter these two commands from the command line:
today=Thursday echo $today
Suppose that the echo command displays Thursday. Now suppose that you write and execute the following shell script named whatday:
# Name: whatday # display the current value of the variable today echo Today is $today. # set the value of today today=Friday # display the current value of the variable today echo Today is $today.
Now enter the following four commands from the command line:
chmod +x whatday today=Thursday whatday echo $today
The following lines appear on-screen:
Today is . Today is Friday. Thursday
The value of the variable today in the login shell is Thursday. When you execute the shell script whatday, you see that initially the variable today isnt defined (as shown by the display Today is .). Then the today variable has the value Friday in the shell. When the whatday script terminates, you return to the login shell and today has its original value, Thursday.
To give the variable today the same value that it has in the login shell when the shell script whatday starts, use the command export. This command exports, or passes on, the variables from one shell to subsequent shells:
export today
Now any shell started from the login shell inherits the value of the variable today. Add the export command to the preceding sequence of commands:
today=Thursday export today whatday echo $today
You see the following output:
Today is Thursday. Today is Friday. Thursday
Notice that the value the variable receives in the shell started by the whatday script isnt carried back to the login shell. Exportation or inheritance of variable values goes in only one directionfrom a running shell down to the new shell, never back up. Thats why when you change your current directory inside one shell, youre back to where you started when that shell terminates.
You can export any variable from one shell down to another shell by using the following syntax:
export variable-name
In this syntax, variable-name is the name of the variable you want to export. To change your terminal type from its current setting to a vt100, for example, enter the following commands to make the new value of TERM available to all subsequent shells or programs:
TERM=vt100 export TERM
When you change or set bash shell variables in the .profile file, be sure to export them. For example, if you want the PATH variable to be PATH=/bin:/usr/bin:/usr/local/bin:., set it in the .profile file and follow it with this export command:
export PATH
To change the shell prompt, you must set a value for PS1 in the file .profile. To change it from $ to Ready $, for example, use a text editor to put these lines in the file named .profile:
PS1=Ready $ export PS1
NOTE: Changes you make to .profile or .login dont take effect until you log out and log in again.
Command aliases are useful for defining commands you use regularly but for which you dont want to bother remembering the details. Command aliases are also useful for enhancing your working environment with a set of useful tools. This command assigns the alias recent to a command that lists the 10 most recently modified files in the current directory:
alias recent=ls -lat|head
To avoid typing your command aliases each time you log in, put them in the .login file if youre using the C shell or the .profile file if youre using bash or a similar shell. The command aliases are now available to you when youre in your shell.
The shell is the primary interface between you and the Linux operating system. Although a shell can be almost any executable program, several standard shells are supplied with Linux or are freely available in source code (written in C) or already compiled for your machine. All Linux shells can be viewed as highly sophisticated, special-purpose programming languages containing all the usual constructs found in a programming language. The special purpose of Linux shell languages is to tie together the many small commands and utilities found in the Linux environment. By making use of I/O redirection and background processing, the shell languages allow you to write complex programs with minimal effort. For more information, see these chapters:
Previous | Table of Contents | Next |