Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 95 ยท Processes and the System

Environment, Profiles and Configuration

Learn the Linux shell environment: env and export, PATH, .bashrc versus .profile, aliases, shopt and set -o, dotfiles, locale and TZ, and the PS1 prompt.

Every program on Linux starts with a small invisible package of settings: the environment. It tells programs where your home directory is, which folders to search for commands, which language and time zone to use, which editor you prefer. The shell has a second layer on top of that, its own options, aliases and prompt. Together they decide how your terminal "feels", and they explain many puzzles: why a command works in one terminal but not in a script, why cron cannot find a program, or why sort orders things differently on two machines.

In this lesson you will learn how the environment is inherited, how to inspect and change it, how PATH works and how to extend it safely, which startup files configure your shell (and in what order), how to define aliases, how to switch shell options with shopt and set -o, how locale and TZ change command output, and how the prompt variable PS1 works. It builds on Variables and Environment Variables and Subshells, Groups and Environment.

Environment variables are inherited copies

An environment variable is a named string that is passed from a parent process to every child it starts. A normal shell variable lives only in the shell; export marks it for the environment.

TEXT
   shell                                  child program (e.g. printenv)
   +--------------------------+  start   +--------------------------+
   | name=Ada        (shell)  |  ------> | LANG=C.UTF-8             |
   | export LANG=C.UTF-8      |  copies  | EDITOR=nano              |
   | export EDITOR=nano       |  only    |   (no "name")            |
   +--------------------------+  exported+--------------------------+

The copy is one-way: the child may change its own environment, but the parent never sees it. Let us see that in practice with printenv, a program that prints the environment it received.

Bash export decides what a child program sees

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

NAME=value command (no export, no semicolon) sets the variable for that one command only. It is the cleanest way to pass a setting to a program without polluting your shell. To list the whole environment use env or printenv, to look at one value printenv NAME or echo "$NAME", and export -p lists everything marked for export, including names without values. To remove a variable use unset NAME.

A clean environment

env can also run a program in a modified environment. With -i it starts with an empty environment, which is a great way to test whether a script depends on settings you forgot about:

Bash env -i and env -u

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In the bare environment HOME, USER and LANG are all missing. (Our sandbox does not define USER at all, so it is missing in both lines; the sandbox's own HOME and LANG are what the second line shows.) That is exactly the situation of a script run by cron, by a systemd service, or by ssh host command: it starts with a minimal environment, not with the settings from your interactive login. If a script works in your terminal and fails there, run it under env -i to reproduce the problem.

PATH: how the shell finds commands

When you type ls, the shell has to find a file called ls. It does not search the whole disk; it looks in the directories listed in PATH, separated by colons, from left to right, and runs the first match.

TEXT
PATH=/home/ann/bin:/usr/local/bin:/usr/bin:/bin

   $ mytool      ->  /home/ann/bin/mytool     ? found, run it
                 ->  /usr/local/bin/mytool    ? (not looked at)

You can build and test a private PATH in a script without touching the real one:

Bash PATH order decides which program runs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

type -a NAME shows every match in PATH order, and the first one is what runs. The last command shows the typical failure message, type: ls: not found (preceded in a script by the script name and line number): the files are still on disk, but the shell does not look in the right place. (Builtins like echo and cd do not need PATH.)

Changing PATH safely

  • Append or prepend, never overwrite. PATH="$HOME/bin:$PATH" puts your folder first; PATH="$PATH:$HOME/bin" puts it last. PATH="$HOME/bin" alone leaves you without ls.
  • Put your own folder first only if you intend to override system commands. Putting it last is safer.
  • Never put . (the current directory) or an empty entry in PATH. A stray :: or a trailing : means "current directory", and then a malicious ls file in a folder you visit can run instead of the real one.
  • export once. PATH is already exported; assigning it in your startup file is enough.
  • Use hash -r if the shell still remembers an old location: Bash caches where it found commands (hash lists the cache).

A script that is not in PATH must be run with a path: ./deploy.sh. That is not a bug: deploy.sh alone makes the shell search PATH only. See Writing Your First Script.

Startup files: which file configures what?

When Bash starts it may read configuration files. Which ones depends on how it was started, along two axes:

  • Login shell or not. Logging in on a console, over SSH, or bash -l starts a login shell.
  • Interactive or not. A terminal with a prompt is interactive; a script is not.
TEXT
   Login shell (interactive)             Interactive, non-login (new terminal tab)
   ------------------------              ----------------------------------------
   /etc/profile                          /etc/bash.bashrc  (Debian/Ubuntu)
   first found of:                       ~/.bashrc
     ~/.bash_profile
     ~/.bash_login
     ~/.profile

   Non-interactive (scripts)             : none of these (only $BASH_ENV if set)
File Typical content
/etc/profile, /etc/profile.d/*.sh System-wide settings for login shells
~/.profile or ~/.bash_profile Your login settings: PATH, EDITOR, other exported variables
~/.bashrc Interactive things: aliases, functions, prompt, shell options, completion
~/.bash_logout Commands to run when a login shell exits

The simple rule of thumb: environment variables go in ~/.profile, interactive comforts go in ~/.bashrc. Because terminal windows on many desktops start non-login shells, while SSH sessions start login shells, the common trick is to make the login file load ~/.bashrc too. This is a read-only example (we cannot start a real login here):

Terminal
# ~/.profile (read-only example)
if [ -n "$BASH_VERSION" ] && [ -f "$HOME/.bashrc" ]; then
  . "$HOME/.bashrc"
fi

# a private bin folder at the end of PATH, only if it exists
if [ -d "$HOME/bin" ]; then
  PATH="$PATH:$HOME/bin"
fi
export EDITOR=nano

Changes in these files affect only new shells. To apply them to the current one, source the file: source ~/.bashrc (or the shorter . ~/.bashrc).

You can try the idea of "reading a configuration file into the current shell" with a file of your own:

Bash source reads a file into the current shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Running a file starts a child process, so its variables vanish with it. source (or .) executes the commands in the current shell, which is how ~/.bashrc can change your prompt and add aliases.

Aliases

An alias gives a short name to a longer command line. It is pure text substitution done by the shell on the first word of a command. Aliases are meant for interactive use: in non-interactive shells (scripts) they are off unless you enable them with shopt -s expand_aliases, and functions are the better tool inside scripts.

Bash Defining, listing and removing aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Notice how \ls and command ls bypass the alias; that is how you run the real program when an alias hides it. An alias cannot take parameters; if you need arguments in the middle of the command, write a function:

Terminal
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }

Put aliases and functions in ~/.bashrc. Avoid redefining standard commands with different meanings (for example alias rm='rm -rf'); in your muscle memory they will betray you on a machine where the alias does not exist.

Shell options: shopt and set -o

Bash has dozens of switches that change how it behaves. There are two families, for historical reasons:

  • set -o NAME / set +o NAME: POSIX-style options such as errexit (set -e), nounset (-u), pipefail, noclobber (-C), noglob (-f). Turn on with -, off with + (it looks backwards, but it is how the option letters have always worked).
  • shopt -s NAME / shopt -u NAME: Bash's own options such as nullglob, globstar, extglob, nocaseglob, dotglob, expand_aliases, histappend. -s sets, -u unsets.
Bash Two ways to change shell behaviour

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The refused overwrite printed f.txt: cannot overwrite existing file and left the status at 1. shopt with an option name prints its state; shopt -p prints the settings in a form you can paste back. set -o lists the set options, and $- holds the single-letter flags that are active. Options set in a script apply to that shell only, so to make a choice permanent for your terminal, put the command in ~/.bashrc.

Locale and time zone: environment variables that change output

Many commands read environment variables to decide how to sort, format dates or display messages. The two biggest families:

  • Locale: LC_ALL (overrides everything), LC_COLLATE (sorting), LC_CTYPE (character classes), LC_TIME, LC_NUMERIC, LC_MESSAGES, and LANG (the default for all of them). locale prints the current values.
  • TZ: the time zone, for example TZ=UTC or TZ=Asia/Tokyo.
Bash The same command with different settings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

With LC_ALL=C the bytes decide: uppercase letters (65 to 90) sort before all lowercase ones (97 to 122). In en_US.UTF-8 the order follows dictionary rules, so a A b B. That difference breaks scripts that parse sorted data and also makes sort and join disagree. This is why professional scripts that need a stable order write LC_ALL=C sort. (The en_US.UTF-8 locale only works where it is installed; our sandbox has it, a minimal container may not.)

Make output reproducible by setting the variables for a single command: LC_ALL=C TZ=UTC mycommand. It affects nothing else, and it is the same trick that all examples in this tutorial use.

The prompt: PS1

The prompt you see before each command comes from the variable PS1 (the primary prompt). PS2 is the continuation prompt shown for multi-line commands (default > ), PS4 is the prefix of set -x traces (see Debugging Shell Scripts). Inside the value, backslash sequences are replaced by Bash:

Sequence Becomes
\u user name
\h host name (up to the first dot)
\w current directory (~ for your home)
\W base name of the current directory
\$ $ for ordinary users, # for root
\t, \d time, date
\j number of background jobs
\n newline
\[ ... \] wrap colour codes so line editing counts the width correctly

In Bash 4.4+ you can ask Bash to expand a prompt string with ${var@P}, so we can experiment in a script:

Bash Expanding prompt strings with @P

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The coloured example is the usual green user@host, blue directory form. The \[ \] pairs tell Bash that the colour codes between them take no space on screen; leave them out and long lines wrap in the wrong place. In your ~/.bashrc you would write it as PS1='...' (single quotes, so it is evaluated each time the prompt appears). Add PROMPT_COMMAND='history -a' or similar if you want to run a command before every prompt.

Dotfiles: your configuration, in files

Files whose names start with a dot are hidden from a plain ls; configuration for most Unix programs lives in such dotfiles in your home directory: ~/.bashrc, ~/.profile, ~/.gitconfig, ~/.ssh/config, ~/.vimrc, ~/.inputrc and so on. Many programs also follow the XDG convention and keep settings in ~/.config/PROGRAM/.

Treat these files as valuable: they hold years of tuning. Common practice is to keep them in a Git repository (a "dotfiles" repo, see Your First Repository) and link them into place with ln -s, so a new machine is set up in minutes.

Common mistakes

  • Changing PATH without $PATH. PATH=/opt/tool/bin loses everything else.
  • Putting export lines in .bashrc for things scripts need. Non-interactive shells do not read it. Use ~/.profile, or pass values explicitly.
  • Assuming scripts see your aliases. They do not; use full commands or functions.
  • Editing .bashrc and expecting existing terminals to change. Run source ~/.bashrc or open a new terminal.
  • Printing the environment into bug reports. env often contains tokens and passwords. Never paste it unfiltered.
  • Locale surprises. sort, grep ranges like [a-z] and ls order all depend on LC_ALL.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You run name=Ada and then bash -c 'echo "[$name]"'. What does the child shell print?

    name is a shell variable, not an exported one, so the child process does not receive it and the expansion is empty. After export name the child would print [Ada].
  2. 2. Which line is the safe way to add /opt/tools/bin to the end of the search path?

    The new directory is appended to the existing value. The first line replaces the whole search path, the third adds the current directory (a security risk), and the last is not a valid assignment.
  3. 3. Where should exported variables such as EDITOR normally be set so that login shells (for example over SSH) get them?

    ~/.profile (or ~/.bash_profile) is read by login shells and is the place for environment settings. ~/.bashrc is for interactive comforts like aliases and the prompt.
  4. 4. A script works in your terminal but fails under cron because node is not found. What is the most likely cause?

    cron starts jobs with a minimal environment, often PATH=/usr/bin:/bin. Set PATH inside the script or use full paths. Testing with env -i bash script.sh reproduces the problem.
  5. 5. What does LC_ALL=C sort change compared with a plain sort in an English UTF-8 locale?

    In the C locale, sort compares bytes, which gives a predictable, machine-independent order: all uppercase letters before all lowercase letters.

Summary

  • The environment is a set of exported variables copied from parent to child; a child can never change its parent's environment.
  • NAME=value cmd sets a variable for one command; env -i runs a command with a clean environment, which exposes hidden dependencies.
  • PATH is searched from left to right; extend it with PATH="$PATH:dir", never overwrite it, and never include . or empty entries.
  • Login shells read /etc/profile and ~/.profile; interactive non-login shells read ~/.bashrc; scripts read neither. Use source to apply a file now.
  • Aliases are for interactive use; use functions in scripts. shopt and set -o switch shell behaviour on and off.
  • LC_ALL, LANG and TZ change sorting, messages and dates; set them per command to make output reproducible.
  • PS1 defines the prompt with escapes like \u, \h, \w; wrap colours in \[ \].

Next: Users, Groups and sudo