≡ Menu

Environment variable changing on Linux/FreeBSD

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:

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 0 comments… add one }

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: