84

In tcsh, I have the following script working:

#!/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 the equivalent to the tcsh setenv function in Bash?

Is there a direct analog? The environment variables are for locating the executable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pbh101
  • 10,203
  • 9
  • 32
  • 31

5 Answers5

127

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

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 5
    PS: no need for the double quotes in the last one. The shell does not perform word splitting for variable assignments. – Jens May 20 '12 at 12:47
  • 2
    Just for the beginners. I had not understood it directly, here in normal English: interpolate "$X" means here that you already have a variable X that is filled with a value, e.g. "A", and you want to read out that value "A" first, and then assign this value as the value of VAR. You do not want VAR to be a string of "$X" here of course. And then the comment above seems also logical, that you will not even need the "". And for a direct assignment of "A", use 'A'. Please correct me if I got it wrong. – questionto42 Dec 16 '20 at 23:24
  • bonus points for the final note about interpolated vars – skittlebiz Feb 17 '22 at 18:36
41

The reason people often suggest writing

VAR=value
export VAR

instead of the shorter

export VAR=value

is that the longer form works in more different shells than the short form. If you know you're dealing with bash, either works fine, of course.

zaphod
  • 4,561
  • 3
  • 25
  • 26
31

Set a local and environment variable using Bash on Linux

Check for a local or environment variables for a variable called LOL in Bash:

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

Sanity check, no local or environment variable called LOL.

Set a local variable called LOL in local, but not environment. So set 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 $

Variable 'LOL' exists in local variables, but not environment variables. LOL will disappear if you restart the terminal, logout/login or run exec bash.

Set a local variable, and then clear out 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 could also just unset the 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 $

Local variable LOL is gone.

Promote a local variable to an 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 exporting makes it show up as both a local variable and an environment variable.

Exported variable DOGE above survives a 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'

Unset all environment variables:

You have to pull out a can of Chuck Norris to reset all environment variables without a logout/login:

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 could set and unset an environment variable manually like this:

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 $
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
13

VAR=value sets VAR to value.

After that export VAR will give it to child processes too.

export VAR=value is a shorthand doing both.

iny
  • 7,339
  • 3
  • 31
  • 36
  • 3
    doesn't it give it to parent processes, not childs? If a shell script does an export, then the shell I invoked it with has that variable, IIRC. – rmeador Oct 24 '08 at 19:03
  • 3
    No. "are marked for automatic export to the environment of subsequently executed commands.", meaning child processes executed after the export. – iny Oct 24 '08 at 19:09
  • 1
    out of curiousity, where is that citation from? – pbh101 Oct 24 '08 at 21:41
8

I think you're looking for export - though I could be wrong.. I've never played with tcsh before. Use the following syntax:

export VARIABLE=value
Oli
  • 235,628
  • 64
  • 220
  • 299