Home Can a shell script set environment variables of the calling shell?

# Can a shell script set environment variables of the calling shell?

user7368
1#
user7368 Published in September 21, 2018, 8:13 am

I'm trying to write a shell script that, when run, will set some environment variables that will stay set in the caller's shell.

setenv FOO foo


in csh/tcsh, or

export FOO=foo


in sh/bash only set it during the script's execution.

source myscript


will run the commands of the script rather than launching a new shell, and that can result in setting the "caller's" environment.

But here's the rub:

I want this script to be callable from either bash or csh. In other words, I want users of either shell to be able to run my script and have their shell's environment changed. So 'source' won't work for me, since a user running csh can't source a bash script, and a user running bash can't source a csh script.

Is there any reasonable solution that doesn't involve having to write and maintain TWO versions on the script?

• @eusoubrasileiro that's not working (at least on osx), as 'export' is interpreted by bash as a file name. – drevicko Jan 12 '16 at 11:48
• see @Humberto Romero 's answer stackoverflow.com/a/28489593/881375 in this thread – tomasb Jul 14 '16 at 13:02