Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a shell script set environment variables of the calling shell? [duplicate]

Tags:

bash

shell

csh

tcsh

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.

I already know that

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?

like image 541
Larry Gritz Avatar asked Jan 30 '09 18:01

Larry Gritz


People also ask

Can you set environment variables in bash script?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “. bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.

Does shell script inherit environment variable?

Shell variables are variables that apply to our current shell only and are not inherited by any programs or scripts that we execute.

Are environment variables shell specific?

Shell variables are only present in the shell in which they were defined. Environment variables are inherited by child shells but shell variables are not. Shell variable can be made an environment variable by using export command. A script is simply a collection of commands that are intended to run as a group.

How do I set an environment variable in shell?

To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.


1 Answers

Use the "dot space script" calling syntax. For example, here's how to do it using the full path to a script:

. /path/to/set_env_vars.sh 

And here's how to do it if you're in the same directory as the script:

. set_env_vars.sh 

These execute the script under the current shell instead of loading another one (which is what would happen if you did ./set_env_vars.sh). Because it runs in the same shell, the environmental variables you set will be available when it exits.

This is the same thing as calling source set_env_vars.sh, but it's shorter to type and might work in some places where source doesn't.

like image 87
Humberto Romero Avatar answered Oct 14 '22 03:10

Humberto Romero