Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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"?

share|improve this question
4  
What did you try already? –  l0b0 Sep 28 '11 at 11:25
1  
editing .bashrc, .bash_profile, .profile and .ssh/eviroment. Although i dont know if i wrote it in the right location or had the right syntax each time –  acidzombie24 Sep 28 '11 at 11:33
1  
.bashrc is the main place for that. –  rozcietrzewiacz Sep 28 '11 at 11:58
    
@rozcietrzewiacz: ok but... even after having the solution i tried setting ABC1 and ABC2 at the start and end of that file and it isnt set in my environment. –  acidzombie24 Sep 28 '11 at 12:31
1  
Check that .bash_profile contains something like [[ -f ~/.bashrc ]] && . ~/.bashrc. –  rozcietrzewiacz Sep 28 '11 at 12:38
show 2 more comments

5 Answers

up vote 31 down vote accepted

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.

share|improve this answer
3  
The default behavior of su user is to change ownership of the current shell to the username arg, while keeping the home directory and environment of the calling user. To enter a shell in such a way as to become the new user, and gain all of their environmental settings ( $PATH, $HOME, $ABC, etc) you need to pass a - as the first argument to su. su - username will accomplish what you are asking for. –  Tim Kennedy Sep 28 '11 at 11:35
1  
Wait: what do you mean by "exited to root"? In any case making an 'su' does not initialize the shell. You need 'su -' (see man su) –  Matteo Sep 28 '11 at 11:43
1  
Why did you su in the first place? Didn't you just create a root-owned .profile that cannot be used by the user? –  rozcietrzewiacz Sep 28 '11 at 11:50
1  
@acid this is what is should have done. The question is, what is sourced when you log in. To check that, log out first. –  rozcietrzewiacz Sep 28 '11 at 11:55
2  
Because .profile is read by all Bourne-compatible shells, you should not write Bash syntax in that file. In particular, export VARIABLE=value needs to be refactored into VARIABLE=value; export VARIABLE. –  tripleee Jun 6 '13 at 6:56
show 5 more comments

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"
ip=127.0.0.1
HORRIBLE=1
share|improve this answer
add comment

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

ABC="123"

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.

share|improve this answer
1  
+1 for the source command. That was missing in all the answers –  Nerrve Mar 29 '13 at 14:16
add comment

Exact command is:

echo 'export ABC = "123"' >> ~/.profile
share|improve this answer
add comment

Use export.

> export ABC="123"

To check if it's set, use

> env
> env | grep ABC

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

export ABC="123"
share|improve this answer
4  
He wants to know hot to define it permanently, i.e., in which file to put the definition. –  Matteo Sep 28 '11 at 11:08
    
Doesnt work. Once i log out the variable is gone. I need it across sessions –  acidzombie24 Sep 28 '11 at 11:08
1  
you mean permenate and not permenently :D –  whoami Sep 28 '11 at 11:09
    
look at edit for bashrc –  whoami Sep 28 '11 at 11:12
    
I tried .bashrc and .bash_profile with no luck –  acidzombie24 Sep 28 '11 at 11:22
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.