128

I wish to write a shell script to export variables.

Below I have listed the script .

echo "Perform Operation in su mode"
export ARCH=arm
echo "Export ARCH=arm Executed"
export PATH='/home/linux/Practise/linux-devkit/bin/:$PATH';
echo "Export path done"
export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-';
echo "Export CROSS_COMPILE done"

But this doesn't seem to work properly. I have to individually execute the commands at the shell prompt instead.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Prag Rao
  • 1,411
  • 3
  • 11
  • 10
  • Which shell are you using (e.g. bash, ksh, sh) – Tom Aug 31 '13 at 11:58
  • possible duplicate of [Save Global variables BASH](http://stackoverflow.com/questions/10779771/save-global-variables-bash) – tripleee Sep 01 '13 at 11:24
  • 2
    Possible duplicate of [Export a variable to the environment from a bash script without sourcing it](http://stackoverflow.com/questions/16618071/export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-it) – user513951 Dec 17 '15 at 19:41
  • 1
    Possible duplicate of [export not working in my shell script](http://stackoverflow.com/questions/10781824/export-not-working-in-my-shell-script) – user Mar 30 '17 at 12:42

5 Answers5

318

You need to run the script as source or the shorthand .

source ./myscript.sh

or

. ./myscript.sh

This will run within the existing shell, ensuring any variables created or modified by the script will be available after the script completes.

Running the script just using the filename will execute the script in a separate subshell.

mbaird
  • 4,059
  • 2
  • 16
  • 14
  • 2
    by using source ./setenv.sh -: Gives the source not found error. and by using . ./setenv.sh it gives can't open setenv.sh. Can you please help me on this. Thanks – Vishal May 14 '15 at 06:21
  • 3
    was scratching my head for an hour why those damn export vars arent there (zsh) tried `typeset -gx`, export etc. and was having my wtf? moment and yet - didnt knew that a subshell is executed with its own scope. thanks from saving my day from impending anger miss-management – Inoperable Apr 25 '17 at 07:09
  • Is there a way to run a script with inherited variables but not give it permission to mutate variables in the outer shell? – Gunar Gessner Jun 29 '21 at 12:35
60

Please show us more parts of the script and tell us what commands you had to individually execute and want to simply.

Meanwhile you have to use double quotes not single quote to expand variables:

export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"

Semicolons at the end of a single command are also unnecessary.

So far:

#!/bin/sh
echo "Perform Operation in su mode"
export ARCH=arm
echo "Export ARCH=arm Executed"
export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"
echo "Export path done"
export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-' ## What's next to -?
echo "Export CROSS_COMPILE done"
# continue your compilation commands here
...

For su you can run it with:

su -c 'sh /path/to/script.sh'

Note: The OP was not explicitly asking for steps on how to create export variables in an interactive shell using a shell script. He only asked his script to be assessed at most. He didn't mention details on how his script would be used. It could have been by using . or source from the interactive shell. It could have been a standalone scipt, or it could have been source'd from another script. Environment variables are not specific to interactive shells. This answer solved his problem.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    HI konsolebox !! Thank you very much . but i didnt understand the difference .. export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH" in double quotes. & export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-' in single quotes. the script modified by u works fine. – Prag Rao Sep 14 '13 at 12:08
  • @PragRao Paramater expansion simply doesn't happen in single quotes. You can have explained details about quoting further [here](https://www.gnu.org/software/bash/manual/html_node/Quoting.html). – konsolebox Sep 14 '13 at 12:19
  • 3
    @konsolebox question name sounds like "Shell script to set environment variables" - the most part of community found this question just to solve problem of running this kind of script from the current shell (`source` or `.`). I appreciate that you answer exactly on OP's question, but I found this topic from google, and the next answer helped me more than yours. Upvotes for the second answer (with `source` and `.` confirm my opinion... – avtomaton Jul 01 '15 at 15:25
  • @avtomaton Again, it's not about other people who finds it on Google. It's only about the original approach of the question. If you have concerns about how a question should be changed, or how an answer should be presented, consider discussing it in meta. – konsolebox Jul 02 '15 at 10:55
6

Run the script as source= to run in debug mode as well.

source= ./myscript.sh
Anurag Mondal
  • 57
  • 1
  • 6
0

I cannot solve it with source ./myscript.sh. It says the source not found error.
Failed also when using . ./myscript.sh. It gives can't open myscript.sh.

So my option is put it in a text file to be called in the next script.

#!/bin/sh
echo "Perform Operation in su mode"
echo "ARCH=arm" >> environment.txt
echo "Export ARCH=arm Executed"
export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"
echo "Export path done"
export "CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-' ## What's next to -?" >> environment.txt
echo "Export CROSS_COMPILE done"
# continue your compilation commands here
...

Tnen call it whenever is needed:

while read -r line; do
    line=$(sed -e 's/[[:space:]]*$//' <<<${line})
    var=`echo $line | cut -d '=' -f1`; test=$(echo $var)
    if [ -z "$(test)" ];then eval export "$line";fi
done <environment.txt
eQ19
  • 9,880
  • 3
  • 65
  • 77
  • 1
    You could modify `myscript.sh` so that it generates a file called `environment.sh` (with the export statements) then just source `environment.sh` instead of the while loop whenever is needed. – slajma Sep 02 '19 at 11:34
0

In my case, I gave extra spaces before and after =. For example, in my shell file(say deploy.sh)

I initially write

GIT_SHA = $(git rev-parse HEAD)

But I fixed it by using:

GIT_SHA=$(git rev-parse HEAD)

So please note that we should not give any spaces before and after the =.

vagdevi k
  • 1,478
  • 9
  • 25