Setting environment variables on Linux using Bash

In tcsh , I have the following working script:

 #!/bin/tcsh setenv X_ROOT /some/specified/path setenv XDB ${X_ROOT}/db setenv PATH ${X_ROOT}/bin:${PATH} xrun -d xdb1 -i $1 > $2 

What is equivalent to tcsh setenv function in bash?

Is there a direct analogue? Environment variables are a search for an executable file.

+56
linux unix bash shell
Oct 24 '08 at 18:25
source share
5 answers

export VAR=value set the VAR value. Include it in single quotes if you want spaces, for example export VAR='my val' . If you want the variable to be interpolated, use double quotes, for example export VAR="$MY_OTHER_VAR" .

+86
Oct 24 '08 at 18:29
source share

The reason people often suggest writing

 VAR=value export VAR 

instead of shorter

 export VAR=value 

lies in the fact that a longer form works in more different shells than in a short form. If you know that you are dealing with bash , then of course it works fine.

+28
Oct 24 '08 at 21:46
source share

Set local and environment variable using Linux Bash

Check local or environment variables for the LOL variable in Bash:

 el@server /home/el $ set | grep LOL el@server /home/el $ el@server /home/el $ env | grep LOL el@server /home/el $ 

A health check, a local variable or an environment variable called LOL.

Set a local variable named LOL in local, but not in the environment. Therefore install it:

 el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ env | grep LOL el@server /home/el $ 

The LOL variable exists in local variables, but not in environment variables. LOL will disappear if you restart the terminal, log off / log in, or run exec bash .

Set local variable and then clear all local variables in Bash

 el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ exec bash el@server /home/el $ set | grep LOL el@server /home/el $ 

You can also just disable one variable:

 el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ unset LOL el@server /home/el $ set | grep LOL el@server /home/el $ 

The local LOL variable is missing.

Contribute to the local variable of the environment variable:

 el@server /home/el $ DOGE="such variable" el@server /home/el $ export DOGE el@server /home/el $ set | grep DOGE DOGE='such variable' el@server /home/el $ env | grep DOGE DOGE=such variable 

Note that the export makes it displayable both as a local variable and as an environment variable.

The exported DOGE variable above survives bash reset:

 el@server /home/el $ exec bash el@server /home/el $ env | grep DOGE DOGE=such variable el@server /home/el $ set | grep DOGE DOGE='such variable' 

Discard all environment variables:

You have to pull the Chuck Norris jar to reset all environment variables without logging out / logging in:

 el@server /home/el $ export CAN="chuck norris" el@server /home/el $ env | grep CAN CAN=chuck norris el@server /home/el $ set | grep CAN CAN='chuck norris' el@server /home/el $ env -i bash el@server /home/el $ set | grep CAN el@server /home/el $ env | grep CAN 

You created an environment variable and then reset the terminal to get rid of them.

Or you can manually set and disable the environment variable as follows:

 el@server /home/el $ export FOO="bar" el@server /home/el $ env | grep FOO FOO=bar el@server /home/el $ unset FOO el@server /home/el $ env | grep FOO el@server /home/el $ 
+18
Aug 20 '14 at 15:37
source share

VAR=value sets the VAR value to value.

After that, the export VAR will also pass it to the child processes.

export VAR=value is a shorthand that does both.

+10
Oct 24 '08 at 18:47
source share

I think you are looking for export - although I could be wrong. I have never played with tcsh before. Use the following syntax:

 export VARIABLE=value 
+6
Oct 24 '08 at 18:28
source share



All Articles