133

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.

4

5 Answers 5

330

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.

3
  • 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, 2015 at 6: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, 2017 at 7:09
  • Is there a way to run a script with inherited variables but not give it permission to mutate variables in the outer shell? Jun 29, 2021 at 12:35
62

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.

4
  • 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, 2013 at 12:08
  • @PragRao Paramater expansion simply doesn't happen in single quotes. You can have explained details about quoting further here.
    – konsolebox
    Sep 14, 2013 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 1, 2015 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 2, 2015 at 10:55
6

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

source= ./myscript.sh
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
1
  • 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 2, 2019 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 =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.