Hermit Notebook

Memo on manipulating Linux environment variables

Memo on manipulating Linux environment variables

Reminder: you can left-scroll and right-scroll the long code blocks.

Some common environment variables

  • PATH : list of paths to be searched for commands.
  • USER : current logged in user.
  • HOME : current user’s home directory.
  • BASH_VERSION : bash version
  • SHELL : current user’s shell path.
  • EDITOR : default file editor used when we type edit in the terminal.
  • LOGNAME : name of the current user.
  • LANG : current locales settings.
  • TERM : current terminal emulation.
  • MAIL : current user’s email storing location.

Display environment variable values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# display the USER env var
printenv USER

# display both HOME SHELL env vars
printenv HOME SHELL

# display using echo
echo $PATH

# list all env vars
printenv

# list all: environment, shell and variables, and shell functions
set | less

Set environment variables for a shell session

1
2
3
# set FOO_VAR and BAR_VAR in the current shell session only
export FOO_VAR='foo'
export BAR_VAR='var'

Set environment variables permanently

You can edit one these files:

  • ~/.bashrc : per-user environment variables loaded by the bash shell. Use source ~/.bashrc to load the new vars in the current shell.
    1
    2
    export USER_VAR1="foo"
    export USER_VAR2="bar"
  • /etc/profile : environment variables loaded by the bash shell for all users.
    1
    2
    export SHELL_VAR1="foo"
    export SHELL_VAR2="bar"
  • /etc/environment : system-wide environment variables. ⚠️ In this file, we don’t write export.
    1
    2
    ENV_VAR1="foo"
    ENV_VAR2="bar"

That’s all for this memo. Leave a comment ✍️ and a “Like” ❤️ if you found this post useful 😉

Thanks for reading !

See you soon !

Keep learning !

Contents

  1. 1. Some common environment variables
  2. 2. Display environment variable values
  3. 3. Set environment variables for a shell session
  4. 4. Set environment variables permanently