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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum