How can you permanently set environment variables using a bash terminal?

You can set an environment variable on a Linux, Windows, or Mac platform with the instructions given below.

If you want to use Schrödinger applications using the command line, see this cheat sheet for an overview.

Linux

To set an environment variable on Linux, enter the following command at a shell prompt, according to which shell you are using:

csh/tcsh: setenv variable value
bash/ksh: export variable=value

where variable is the name of the environment variable (such as SCHRODINGER) and value is the value you want to assign to the variable, (such as /opt/schrodinger2019-3). To find out what environment variables are set, use the env command. To remove a variable from the environment, use the following commands:

csh/tcsh: unsetenv variable
bash/ksh: export -n variable

Windows

You can create or change environment variables in the Environment Variables dialog box. If you are adding to the PATH environment variable or any environment variable that takes multiple values, you should separate each value with a semicolon (;).

Windows 8 and Windows 10

To open the Environment Variables dialog box:

  1. In Search, search for and then select: Edit environment variables for your account

To create a new environment variable:

  1. In the User variables section, click New to open the New User Variable dialog box.
  2. Enter the name of the variable and its value, and click OK. The variable is added to the User variables section of the Environment Variables dialog box.
  3. Click OK in the Environment Variables dialog box.

To modify an existing environment variable:

  1. In the User variables section, select the environment variable you want to modify.
  2. Click Edit to open the Edit User Variable dialog box.
  3. Change the value of the variable and click OK. The variable is updated in the User variables section of the Environment Variables dialog box.

When you have finished creating or editing environment variables, click OK in the Environment Variables dialog box to save the values.

Windows 7

To open the Environment Variables dialog box:

  1. Click Start, then click Control Panel
  2. Click User Accounts.
  3. Click User Accounts again.
  4. In the Task side pane on the left, click Change my environment variables to open the Environment Variables dialog box opens.

To create a new environment variable:

  1. In the User variables section, click New to open the New User Variable dialog box.
  2. Enter the name of the variable and its value, and click OK. The variable is added to the User variables section of the Environment Variables dialog box.
  3. Click OK in the Environment Variables dialog box.

To modify an existing environment variable:

  1. In the User variables section, select the environment variable you want to modify.
  2. Click Edit to open the Edit User Variable dialog box opens.
  3. Change the value of the variable and click OK. The variable is updated in the User variables section of the Environment Variables dialog box.

When you have finished creating or editing environment variables, click OK in the Environment Variables dialog box to save the values. You can then close the Control Panel.

macOS

To set an environment variable on macOS, open a terminal window. If you are setting the environment variable to run jobs from the command line, use the following command:

export variable=value

where variable is the name of the environment variable (such as SCHRODINGER) and value is the value you want to assign to the variable, (such as /opt/schrodinger/suites2019-3). You can find out which environment variables have been set with the env command.

If you are setting the environment variable globally to use with applications, use the commands given below. The environment variables set by these commands are inherited by any shell or application.

macOS newer than 10.10

See this article for instructions on how to create a "plist" file to store system-wide environment variables in newer versions of macOS.

MacOS X 10.10

To set an environment variable, enter the following command:

launchctl setenv variable "value"

To find out if an environment variable is set, use the following command:

launchctl getenv variable

To clear an environment variable, use the following command:

launchctl unsetenv variable

Setting environment variables in Linux is a good way to define common and repetitive variables that are used across a number of applications and terminal commands. These environment variables help in cutting down verbosity, bring ease of use, and make development workflow better.

An environment variable in Linux can be used to pass information or influence the behavior of an application or a process. This article will explain various ways to set environment variables and how to use them.

To set an environment variable for the shell currently in use, define a variable in the following format:

The definition is self explanatory, “MYVAR” is the variable name while “xyz” is its value. Running the command below will verify if the environment variable has been set correctly:

How can you permanently set environment variables using a bash terminal?

Notice the syntax of environment variables. While they work like any other shell variables, it is generally a good practice to use capital letters and underscores for left hand side (variable name).

To unset a variable, use the command below:

If you check the variable again using the echo command mentioned above, no output will be displayed. Note that unset will work for current terminal session only. If there are any global, system wide environment variables defined on your system, they will be available again in a new terminal session.

To set an environment variable for the shell currently in use and also for any child processes / shells launched from it, use a variable in the following format:

To permanently set an environment variable for bash shells (most default terminal apps in Linux distributions are configured for bash shell), add the variable (with “export” keyword) at the end of the hidden .bashrc file in your home directory.

You can edit .bashrc file by running the command below:

Replace “subl” with the command of your favorite text editor. You will need to reload .bashrc file to enable the changes. Run the command below to do so:

Below is an example of custom environment variables I have set for Ruby Gems.

How can you permanently set environment variables using a bash terminal?

You can view all environment variables enabled on your system by running the command below:

To specifically check if the custom environment variable added to .bashrc file has been enabled or not, run the command below:

How can you permanently set environment variables using a bash terminal?

To set an environment variable system wide for all apps, shells and processes, add your custom variable in “/etc/environment” file without “export” keyword.

You can edit “/etc/environment” file by running the command below:

$ sudo subl “/etc/environment”

Replace “subl” with your favorite text editor. You may need to reboot the system for the changes to take effect. To verify if your custom variable has been set correctly, run the command below:

Alternatively, you can use “printenv” command to verify the changes:

How can you permanently set environment variables using a bash terminal?

Note that the “unset” command explained above works for all custom environment variables, whether they are session specific or global variables. However, unset removes a variable for the running shell session only and it won’t remove any system wide or global variable permanently.

Some of the predefined environment variables in Ubuntu include:

  • USER – name of the logged-in user
  • HOME – home directory of logged in user (usually /home/username)
  • DISPLAY – active monitor in use (usually automatically set by login manager)
  • PWD – working directory where the shell is being used or invoked
  • SHELL – shell that is being used system wide (usually /bin/bash)
  • LANG – language used by the system (user defined, can be changed)
  • PATH – scripts / binaries / executables are searched in the directories set in the PATH variable

Some of the environment variables that are commonly used to influence application behavior:

  • LC_ALL – force overrides user defined locale with the value specified in the variable
  • LD_LIBRARY_PATH – used to define additional directories where runtime libraries will be searched
  • PATH – used to define additional directories where scripts / binaries / executables will be searched
  • LD_PRELOAD – used to load custom / downgraded / upgraded libraries in an application

This marks the end of this article. Environment variables in Linux helps in running tweaked commands and applications without actually modifying underlying source and binaries by providing a way to define and use global variables across the system.

About the author

How can you permanently set environment variables using a bash terminal?

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.

How do I set a permanent environment variable in Bash?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “.bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.

What are shell environment variables in Linux?

There’s a third set of environment variables defined within the shell. Your locale, time zone, and keyboard settings, the set of directories searched when the shell tries to find a command, and your default editor, are all stored in shell environment variables.

How to set environment variables for a single user in Linux?

To set permanent environment variables for a single user, edit the .bashrc file: 2. Write a line for each variable you wish to add using the following syntax: 3. Save and exit the file. The changes are applied after you restart the shell.

How to set variables in /etc/Environment?

In /etc/environment, variables are usually set with name=value, eg: In /etc/profile, you must use export since this is a script, eg: Same goes for a file under /etc/profile.d, there also may be naming restrictions which must be met for the file to work.

How can you permanently set an environment variable using a bash terminal?

You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.

Are Linux environment variables permanent?

Set an Environment Variable in Linux Permanently If you wish a variable to persist after you close the shell session, you need to set it as an environmental variable permanently. You can choose between setting it for the current user or all users. 6.