Aug
Aug

Reputation: 599

setenv bash command not working in shell

I am trying to simplify my job by writing a bash file . When I try this code in Terminal :

env | grep $TMPDIR

it returns:

/tmp

( I don't know where this is set and couldn't find the line of code assigning this /tmp path to TMPDIR. I would be also great if someone can tell me how to find the file containing this assignment ).

Everything works fine when I set my correct path in the Terminal like this:

export TMPDIR=/My/Path/to/where/I/need

but it does not work when I use the same code in a bash file:

#!/bin/bash
setenv TMPDIR /My/Path/to/where/I/need

I also tried these:

setenv TMPDIR "/My/Path/to/where/I/need"

or:

export TMPDIR "/My/Path/to/where/I/need"

all of them return "/tmp" in response to echo $TMPDIR

Any suggestions?

Upvotes: 0

Views: 11023

Answers (2)

chw21
chw21

Reputation: 8140

I assume that you are trying to run a shell script that sets an environment variable in the calling environment, as in:

$ echo $myvar
old_value
$ ./set_new_value.sh
$ echo $myvar
new_value

Firstly, you need to use export, not setenv.

Secondly, you need to source the script:

$ cat set_new_value.sh
export myvar='new_value'
$ source ./set_new_value.sh
$ echo $myvar
new_value

You can also use the . alone:

$ . ./set_new_value.sh

Cheers, Holger

Upvotes: 1

Martin Tournoij
Martin Tournoij

Reputation: 27852

setenv is for csh only. Use export in Bourne shells.

Unlike csh's setenv, you need a = between the key and the value:

export TMPDIR="/My/Path/to/where/I/need"

Upvotes: 3

Related Questions