Linux Variables

You can declare a variable by typing in the terminal: VARIABLE_NAME="variable value"

However, it remains as a variable as long as that terminal session exists. You can keep it around by manipulating your .bashrc config file (probably found in your $HOME / ~ directory), or other bash configuration files.


Types:

  • Local Variables
  • Environment Variables
  • Shell Variables
    • Special variable that is assigned and required by the shell. They can be local or environment variables.

Commands

Use printenv to show a list of environment variables. 

You can specify the variable to see just that value: printenv HOME

You can also use env by itself to see the same list of variables. However, env can also do so much more, while printenv just prints the variables.

Typing set will give you the same list as both printenv and env, but so much more. 

The export command will set the variable. 


Common Environment Variables

HOME home directory of $USER; also what ~ refers to
USER you, or the current logged in user
PATH List of directories that are referenced when you type a command. If I type ls, this list of directories is searched in order to execute that command. (You can see the path of ls by typing which ls)
SHELL The current shell you are using, like bash or zsh
LS_COLORS



  • Persistent Variables:

    • /etc/environment
  • System wide environment variables. You may find $PATH in here. 

    • /etc/profile
    • .bashrc