linux - [Solved-5 Solutions] Can a shell script set environment variables of the calling shell ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

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

Linux - Solution 1:

  • Your shell process has a copy of the parent's environment and no access to the parent process's environment whatsever.
  • When your shell process terminates any changes you've made to its environment are lost.
  • Sourcing a script file is the most commonly used method for configuring a shell environment, you may want to bite the bullet and maintain one for each of the two flavors of shell.

Linux - Solution 2:

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
click below button to copy the code. By - Linux tutorial - team

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

. set_env_vars.sh
click below button to copy the code. By - Linux tutorial - team

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.

Linux - Solution 3:

When child processes inherit your shell's variables, they're inheriting copies themselves.

One thing you can do is to write a script that emits the correct commands for tcsh or sh based how it's invoked. If you're script is "setit" then do:

ln -s setit setit-sh
click below button to copy the code. By - Linux tutorial - team

and

ln -s setit setit-csh
click below button to copy the code. By - Linux tutorial - team

Now either directly or in an alias, you do this from sh

eval `setit-sh`
click below button to copy the code. By - Linux tutorial - team

or this from csh

eval `setit-csh`
click below button to copy the code. By - Linux tutorial - team

setit uses $0 to determine its output style.

The advantage here is that setit is just written in whichever shell you like as in:

#!/bin/bash
arg0=$0
arg0=${arg0##*/}
for nv in \
   NAME1=VALUE1 \
   NAME2=VALUE2
do
   if [ x$arg0 = xsetit-sh ]; then
      echo 'export '$nv' ;'
   elif [ x$arg0 = xsetit-csh ]; then
      echo 'setenv '${nv%%=*}' '${nv##*=}' ;'
   fi
done
click below button to copy the code. By - Linux tutorial - team

with the symbolic links given above, and the eval of the backquoted expression, this has the desired result.

To simplify invocation for csh, tcsh, or similar shells:

alias dosetit 'eval `setit-csh`'
click below button to copy the code. By - Linux tutorial - team

or for sh, bash, and the like:

alias dosetit='eval `setit-sh`'
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

In .bash_profile :

# No Proxy
function noproxy
{
    /usr/local/sbin/noproxy  #turn off proxy server
    unset http_proxy HTTP_PROXY https_proxy HTTPs_PROXY
}


# Proxy
function setproxy
{
    sh /usr/local/sbin/proxyon  #turn on proxy server 
    http_proxy=http://127.0.0.1:8118/
    HTTP_PROXY=$http_proxy
    https_proxy=$http_proxy
    HTTPS_PROXY=$https_proxy
    export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
}
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

$ cat setfoo
#! /bin/bash

gdb /proc/${PPID}/exe ${PPID} <<END >/dev/null
call setenv("foo", "bar", 0)
END
$ echo $foo

$ ./setfoo
$ echo $foo
bar
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - Can a shell script set environment variables of the calling shell ?