1.17. Shell Variables

In addition to the environment, shells maintain a similar set of variables for their own use. These variables are not passed down to child processes, and are only used by the shell.

Shell variables can be arbitrary, but each shell also treats certain variable names specially. One common example is the shell variable that stores the shell prompt.

In Bourne-shell derivatives, this variable is called PS1. To set a shell variable in Bourne-shell derivatives, we use a simple assignment. ( The export command above actually sets a shell variable called TERM and then exports it to the environment. )

shell-prompt: PS1="peregrine: "
	

In C shell derivatives, the variable is called prompt, and is set using the set command:

shell-prompt: set prompt="peregrine: "
	

Note

The syntax for set is slightly different than for setenv. Set uses an '=' while setenv uses a space.

Shell prompt variables may contain certain special symbols that represent dynamic information they you might want to include in your shell prompt, such as the host name, command counter, current working directory, etc. Consult the documentation for your shell for details.

In all shells, you can view the current shell variables by typing set with no arguments:

shell-prompt: set
	

1.17.1. Self-test

  1. Show how to set the shell prompt to "Peregrine: " in:
    1. Bourne shell
    2. C shell
  2. How can you view a list of all current shell variables and their values?