What are Variables?
A variable is a temporary storage area in memory that is either set by the user, shell, system, or any program that loads another program. There are two categories of variables:
- The environment variables are valid for the duration of the session.
- The shell variables apply only to the current instance of the shell and are used to set short-term working conditions.
Displaying Shell Variables
The echo command displays the value stored inside a shell variable.
$ echo $SHELL /bin/bash
$ set DISPLAY=:0.0 EDITOR=/usr/bin/vi SHELL=/bin/bash TERM=xterm-256color USER=geek ...
Setting and Unsetting Shell Variables
Shell variables are set using the set command. For example:
$ set history = 50 $ echo $history 30
The set command is also used to display the shell variables and their values. For example:
$ set | grep history 30
The values can be reversed by using the unset command.
Default Bash Shell Variables
The table describes variables that are assigned default values by the bash shell on login.
Variable | Meaning |
---|---|
EDITOR | Defines the default editor for the shell |
FCEDIT | Defines the editor for the fc command. Used with the history mechanism for editing previously executed commands. |
HOME | Sets the directory to which the cd command changes when no argument is supplied on the command line |
LOGNAME | Sets the login name of the user |
PATH | Specifies a colon-delimited list of directories to be searched when the shell needs to find a command to be executed |
PS1 | Specifies the primary Bash shell prompt: $ |
PS2 | Specifies the secondary command prompt, normally: > |
SHELL | Specifies the name of the shell (that is, /bin/bash) |
Customizing Shell Variables: PS1
The shell prompt string is stored in the shell variable PS1, and you can customize it according to your preference.
$ PS1=”$LOGNAME@`uname -n` \$PWD $ “ user@server01: $
– In this example, the prompt displays the login name of the user, the system’s host name, and the current working directory.
– The username is read from the variable LOGNAME, and the host name comes from the output of the ‘uname -n‘ command.
– This shell prompt displays the correct information even when the user logs in on different hosts.
– The back quotation (`) mark delimits an embedded command string.
Customizing Shell Variables: PATH
The PATH variable contains a list of directory path names, separated by colons. When executing a command on the command line, the shell searches for these directories from left to right, in sequence to locate that command. If the shell does not find the command in the list of directories, it displays a “not found” error message. To ensure that commands operate smoothly, you should include the respective directory in the PATH variable. The example illustrates the inclusion of the home directory into the PATH variable.
For example, to include the home directory in the PATH variable, perform the following commands.
$ echo $PATH /usr/bin:/usr/sbin
$ PATH=$PATH:~
$ echo $PATH /usr/bin:/usr/sbin:/home/user01