Linux environment variables

You can use any one of the following command to display environment variables and their values using bash shell.

  • printenv print all or part of environment variables
  • env print all exported environment or run a program in a modified environment
  • set print the name and value of each shell variable
  • export to set environment variables
  • source to load new environment variables into the current shell session

Samples

This commands will display a large list of variables so you probably want to pipe the output with less or more commands.

$ printenv
SHELL=/bin/bash
SESSION_MANAGER=/local/...
...

$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin/:/usr/bin...

$ printenv PATH LANG
/usr/local/sbin:/usr/local/bin:/usr/sbin/:/usr/bin...
en_US.UTF-8

$ env
SHELL=/bin/bash
SESSION_MANAGER=/local/...
...

$ source ~/.bashrc

$ printenv | less
$ printenv | more

Persistent environment variables

If you need to create a persistent variable in the bash configuration files, it must be defined in one of the following files:

  • /etc/environment variables defined in this file are set up system-wide
FOO=bar
VAR="My new variable"
  • /etc/profile or /home/username/.profile variables defined in this file are loaded when the shell is entered. Variables defined in this file need to use with export command.
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin
  • ~/.bashrc variables defined in this file are user-specific when using Bash shell.
export PATH="$HOME/bin:$PATH"

Deja una respuesta