Environment variable changing on Linux/FreeBSD

Posted on in Categories , , last updated August 28, 2006

Environment variables are a set of values that can affect the way running processes will behave. It is stored in the shell’s environment space. This space can be used by any program invoked by the shell, and thus contains a lot of program configuration. Linux (and FreeBSD/Solaris/UNIX) process use environment variable for different purposes. Running programs can access the values of environment variables for configuration purposes. Examples of environment variables include:

  • PATH : Search for executable files
  • SHELL : Your current shell

You can display values of these environment variables with echo command:

$ echo $PATH
$ echo $HOME

Setting (changing) an environment variable on Linux/FreeBSD
Setting an environment variable differs from shell to shell. Setting an environment variable for csh/tcsh shell on Linux/FreeBSD:
setenv variable value

For example, to set or modify the PAGER environment variable under csh or tcsh you need to type command:

$ setenv PAGER less

Setting an environment variable for sh/bash or ksh shell
export variable=value

For example, to set or modify the EDITOR environment variable under sh or bash shell you need to type command:

$ export EDITOR=vim
$ export EDITOR=/usr/bin/vim

Shell configuration files
These environment variables defined in shell startup files so that when ever you login next time they are reloaded automatically for you. Under Linux bash is the default shell. Following list summaries bash startup files:

  • /etc/profile: The systemwide initialization file, executed when you login into system. Only super-user (root) can make any changes to this file.
  • /etc/bash.bashrc (/etc/bashrc): The systemwide per-interactive-shell startup file. Only super-user (root) can make any changes to this file. Generally, bash.bashrc is called from /etc/profile file.

Use above files to setup system wide variables such as JAVA path. Since normal users do not have access to these file they can use following two files:

  • /home/user/.bash_profile (~/.bash_profile): The personal initialization file, executed when you log into system.
  • /home/user/.bashrc (~/.bashrc): The individual per-interactive-shell startup file

Use text editor such as vi to modify or define new environment variable for you. For example, setup EDITOR variable in your .bash_profile file:

$ vi  ~/.bash_profile


$ cd
$ vi .bash_profile

Add/modify EDITOR as follows:

$ export EDITOR=/usr/bin/vim

Save file logout and login again or just type following command to load environment changes immediately:

$ ./.bash_profile

See also:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.