How do I set a user environment variable? (permanently, not session)

This is irritating me. I seen several suggestions (all using different files and syntax) and none of them worked.

How do I set an environment variable for a specific user? I am on debian squeeze.
What is the exact syntax I should put in the file to make ABC = “123”?

Asked By: user4069


Use export.

export ABC="123"

To check if it’s set, use



env | grep ABC

To add it permanently, add this to your ~/.bashrc file.

export ABC="123"
Answered By: whoami

You have to put the declaration in the initialization files of your shell:

  • If you are using bash, ash, ksh or some other Bourne-style shell, you can add

    ABC="123"; export ABC

    in your .profile file (${HOME}/.profile). This is the default situation on most Unix installations, and in particular on Debian.

    If your login shell is bash, you can use .bash_profile (${HOME}/.bash_profile) or .bash_login instead.

    Note: If either of these files exists and your login shell is bash, .profile is not read when you log in over ssh or on a text console, but it might still be read instead of .bash_profile if you log in from the GUI. Also, if there is no .bash_profile, then use .bashrc.

  • If you’ve set zsh as your login shell, use ~/.zprofile instead of ~/.profile.

  • If you are using tcsh, add

    setenv ABC "123"

    in .login file (${HOME}/.login)

  • if you are using another shell look at the shell manual how to define environment variables and which files are executed at the shell startup.

Answered By: Matteo

This is a general procedure you can use for pretty much any shell. In any case, you have to know which shell the user would normally log in with:

path="$(grep $USER /etc/passwd | cut -d ':' -f 7)"
shell="$(basename -- "$path")"

Then you have to figure out which dot-files this shell would normally read:

man $shell

A shortcut which might work is to list those dot-files which contain the shell name:

ls ~/.*${shell}*

If you want to check if one of the files is actually read during login, you can simply print the file name in each of them, for example:

echo .bashrc

When logging in, you should then see which files are being read, and you can decide which one to modify. Beware that you should not to try to use echo "$0" or similar, because the value of $0 depends on how the shell processes dot-files, and could be misleading.

When it comes to declaring the variable “permanently”, note that this only extends to the session. There is no way to access the value of a variable without a session, so it has no meaning outside of one. If you mean “read-only”, that is shell dependent, and in Bash you can use:

declare -r VAR

if it already has a value, or

declare -r VAR=value

to assign it at the same time. Not all shells have this feature.

To declare a variable in most shells, you should use a variable name ([A-Za-z_][A-Za-z0-9_]*), followed by an equal sign (and no spaces around the equal sign), then a value (preferably quoted unless the value is a simple [A-Za-z0-9_]+). For example:

name="John Doe"
Answered By: l0b0

Use /etc/environment file for setting the environment variables. Then add the following line inside the /etc/environment file.


Now the ABC variable will be accessible from all the user sessions. To test the variable output first refresh the environment variable using command

source /etc/environment

and run echo $ABC.

Answered By: Hokam

Exact command is:

echo 'export ABC = "123"' >> ~/.profile

The best way that worked for me is modifying the .bashrc file in the BASH shell.

use this for example:

  1. open your .bashrc file
  2. add this line:

    export <VARIABLE> = "<ANY PATH/VALUE you want>"

    eg : export HOME = "/home/user/"

    Remember to use "" as using ' ' will not work and your bashrc file will report error while sourcing.

  3. source it in the current session.

I also tried to modify the /etc/environment file but it didn’t allow me to create a new environment variable and when I sourced it, it prompted error that the defined variable is not found.

Answered By: Ayushi Agarwal
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.