Bookmark this page

Change the Shell Environment

Objectives

Set shell variables to run commands, and edit Bash startup scripts to set shell and environment variables to modify the behavior of the shell and programs that are run from the shell.

Shell Variable Usage

With the Bash shell, you can set shell variables to help to run commands or to modify the behavior of the shell. You can also export shell variables as environment variables, which are automatically copied to programs that are run from that shell. You can use variables for ease of running a command with a long argument, or to apply a common setting to commands that are run from that shell.

Shell variables are unique to a particular shell session. If you have two terminal windows open, or two independent login sessions to the same remote server, then you are running two shells. Each shell has its own set of values for its shell variables.

Assign Values to Variables

Assign a value to a shell variable with the following syntax:

[user@host ~]$ VARIABLENAME=value

Variable names can contain uppercase or lowercase letters, digits, and the underscore character (_). For example, the following commands set shell variables:

[user@host ~]$ COUNT=40
[user@host ~]$ first_name=John
[user@host ~]$ file1=/tmp/abc
[user@host ~]$ _ID=RH123

Remember, this change affects only the shell that you run the command in, not any other shells that you might be running on that server.

You can use the set command to list all shell variables that are currently set. (It also lists all shell functions, which you can ignore.) To improve readability, you can pipe the output to the less command so that you can view it one page at a time.

[user@host ~]$ set | less
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote\
:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progc\
omp:promptvars:sourcepath
BASHRCSOURCED=Y
...output omitted...

Retrieve Values with Variable Expansion

You can use variable expansion to refer to the value of a variable that you set. To use variable expansion, precede the name of the variable with a dollar sign ($). In the following examples, the variable expansion occurs first and then the echo command prints the rest of the command line that is entered.

For example, the following command sets the variable COUNT to 40.

[user@host ~]$ COUNT=40

If you enter the echo COUNT command, then it prints the COUNT string.

[user@host ~]$ echo COUNT
COUNT

If you enter instead the echo $COUNT command, then it prints the value of the COUNT variable.

[user@host ~]$ echo $COUNT
40

You can also use a variable to refer to a long file name for multiple commands.

[user@host ~]$ file1=/tmp/tmp.z9pXW0HqcC
[user@host ~]$ ls -l $file1
-rw-------. 1 student student 1452 Jan 22 14:39 /tmp/tmp.z9pXW0HqcC
[user@host ~]$ rm $file1
[user@host ~]$ ls -l $file1
total 0

Important

You can always use curly braces in variable expansion, although they are often unnecessary.

In the following example, the echo command tries to expand the nonexistent variable COUNTx, but returns nothing. The command does not report any errors either.

[user@host ~]$ echo Repeat $COUNTx
Repeat

If any trailing characters are next to a variable name, then delimit the variable name with curly braces. In the following example, the echo command now expands the COUNT variable.

[user@host ~]$ echo Repeat ${COUNT}x
Repeat 40x

Configure Bash with Shell Variables

Some shell variables are set when Bash starts. You can modify them to adjust the shell's behavior.

For example, the HISTFILE, HISTFILESIZE, and HISTTIMEFORMAT shell variables affect the shell history and the history command. The HISTFILE variable specifies which file to save the shell history to, and defaults to the ~/.bash_history file. The HISTFILESIZE variable specifies how many commands to save in that file from the history. The HISTTIMEFORMAT variable defines the time stamp format for every command in the history. This variable does not exist by default.

[user@host ~]$ history
...output omitted...
    6  ls /etc
    7  uptime
    8  ls -l
    9  date
   10  history
[user@host ~]$ HISTTIMEFORMAT="%F %T "
[user@host ~]$ history
...output omitted...
    6  2022-05-03 04:58:11 ls /etc
    7  2022-05-03 04:58:13 uptime
    8  2022-05-03 04:58:15 ls -l
    9  2022-05-03 04:58:16 date
   10  2022-05-03 04:58:18 history
   11  2022-05-03 04:59:10 HISTTIMEFORMAT="%F %T "
   12  2022-05-03 04:59:12 history

Another example is the PS1 variable, which controls the appearance of the shell prompt. If you change this value, then it changes the appearance of your shell prompt. Various special character expansions that the prompt supports are listed in the "PROMPTING" section of the bash(1) man page.

[user@host ~]$ PS1="bash\$ "
bash$ PS1="[\u@\h \W]\$ "
[user@host ~]$

Because the value that the PS1 variable sets is a prompt, Red Hat recommends ending the prompt with a trailing space. Also, whenever a variable value contains some form of space, including a space, a tab, or a return, the value must be enclosed in either single or double quotation marks. Unexpected results might occur if the quotation marks are omitted. The previous PS1 variable conforms to both the trailing space recommendation and the quotation marks rule.

Configure Programs with Environment Variables

The shell provides an environment for the programs that you run from that shell. Among other items, this environment includes information about the current working directory on the file system, the command-line options that are passed to the program, and the values of environment variables. The programs might use these environment variables to change their behavior or their default settings.

If a shell variable is not an environment variable, then only the shell can use it. However, if a shell variable is an environment variable, then the shell and any programs that run from that shell can use the variable.

Note

The HISTFILE, HISTFILESIZE, and PS1 variables from the previous section do not need to be exported as environment variables, because only the shell itself uses them, not the programs that you run from the shell.

You can assign any variable that is defined in the shell as an environment variable by marking it for export with the export command.

[user@host ~]$ EDITOR=vim
[user@host ~]$ export EDITOR

You can set and export a variable in one step:

[user@host ~]$ export EDITOR=vim

Applications and sessions use these variables to determine their behavior. For example, the shell automatically sets the HOME variable to the file name of the user's home directory when it starts. You can use this variable to help programs to determine where to save files.

Another example is the LANG variable, which sets the locale encoding. This variable adjusts the preferred language for program output; the character set; the formatting of dates, numbers, and currency; and the sort order for programs. If it is set to en_US.UTF-8, then the locale uses US English with UTF-8 Unicode character encoding. If it is set, for example, to fr_FR.UTF-8, then it uses French UTF-8 Unicode encoding.

[user@host ~]$ date
Tue Jan 22 16:37:45 CST 2019
[user@host ~]$ export LANG=fr_FR.UTF-8
[user@host ~]$ date
mar. janv. 22 16:38:14 CST 2019

Another important environment variable is PATH. The PATH variable contains a list of colon-separated directories that contain programs:

[user@host ~]$ echo $PATH
/home/user/.local/bin:/home/user/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

When you run a command such as the ls command, the shell looks for the ls executable file in each of those directories in order, and runs the first matching file that it finds. (On a typical system, this file is /usr/bin/ls.)

You can append directories to your PATH variable. For example, perhaps you want to run some executable programs or scripts such as regular commands in the /home/user/sbin directory. You can append the /home/user/sbin directory to your PATH for the current session as follows:

[user@host ~]$ export PATH=${PATH}:/home/user/sbin

To list all the environment variables for a shell, run the env command:

[user@host ~]$ env
...output omitted...
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=host.example.com
XDG_SESSION_ID=4
...output omitted...

Set the Default Text Editor

The EDITOR environment variable specifies your default text editor for command-line programs. Many programs use the vi or vim editor if it is not specified, and you can override this preference:

[user@host ~]$ export EDITOR=nano

Important

By convention, environment variables and shell variables that are automatically set by the shell have names with all uppercase characters. If you are setting your own variables, then you might want to use names with lowercase characters to prevent naming collisions.

Set Variables Automatically

When Bash starts, several text files run with shell commands that initialize the shell environment. To set shell or environment variables automatically when your shell starts, you can edit these Bash startup scripts.

The exact scripts that run depend on whether the shell is interactive or non-interactive, and a login or non-login shell. A user directly enters commands into an interactive shell, whereas a non-interactive shell, such as a script, runs in the background without user intervention. A login shell is invoked when a user logs in locally via the terminal or remotely via the SSH protocol. A non-login shell is invoked from an existing session, such as to open a terminal from the GNOME GUI.

For interactive login shells, the /etc/profile and ~/.bash_profile files configure the Bash environment. The /etc/profile and ~/.bash_profile files also source the /etc/bashrc and ~/.bashrc files respectively. For interactive non-login shells, only the /etc/bashrc and ~/.bashrc files configure the Bash environment. Whereas the /etc/profile and /etc/bashrc files apply to the whole system, the ~/.bash_profile and ~/.bashrc files are user-specific. Non-interactive shells invoke any files that the BASH_ENV variable defines. This variable is not defined by default.

To create a variable that is available to all of your interactive shells, edit the ~/.bashrc file. To apply a variable only once after the user logs in, define it in the ~/.bash_profile file.

For example, to change the default editor when you log in via SSH, you can modify the EDITOR variable in your ~/.bash_profile file:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export EDITOR=nano

Note

The best way to adjust settings that affect all user accounts is to add a file with a .sh extension that contains the changes to the /etc/profile.d directory. To create the files in the /etc/profile.d directory, log in as the root user.

Bash Aliases

Bash aliases are shortcuts to other Bash commands. For example, if you must often type a long command, then you can create a shorter alias to invoke it. You use the alias command to create aliases. Consider the following example, which creates a hello alias for an echo command.

[user@host ~]$ alias hello='echo "Hello, this is a long string."'

You can then run the hello command and it invokes the echo command.

[user@host ~]$ hello
Hello, this is a long string.

Add aliases to a user's ~/.bashrc file so they are available in any interactive shell.

Unset and Unexport Variables and Aliases

To unset and unexport a variable, use the unset command:

[user@host ~]$ echo $file1
/tmp/tmp.z9pXW0HqcC
[user@host ~]$ unset file1
[user@host ~]$ echo $file1

[user@host ~]$

To unexport a variable without unsetting it, use the export -n command:

[user@host ~]$ export -n PS1

To unset an alias, use the unalias command:

[user@host ~]$ unalias hello

 

References

bash(1), env(1), and builtins(1) man pages

Revision: rh124-9.0-398f302