1

When I run the 'set' command in a bash shell, I see a variable has a value. However, when I run the 'set' command in a bash script, the variable does not exist. Why? How might I make this happen?

AxOS(7iq1) root:/mnt/ax/scratch/roshi# set
--snip--
SERIAL_NUMBER=7iq1
--snip--
AxOS(7iq1) root:/mnt/ax/scratch/roshi# 

My shell script tmp.sh contains

#!/bin/bash
svcid=`set | grep ^SERIAL_NUMBER | awk '{ split($1,a,"=");print a[2] }'`
echo ${svcid}

If I execute the script as follows, I get no output

AxOS(7iq1) root:/mnt/ax/scratch/roshi# ./tmp.sh

AxOS(7iq1) root:/mnt/ax/scratch/roshi# 

If I execute the script as (first suggested by Doon)

AxOS(7iq1) root:/mnt/ax/scratch/roshi# . ./tmp.sh
7iq1
AxOS(7iq1) root:/mnt/ax/scratch/roshi# 
5
  • 1
    Please post a (small) example Commented Apr 26, 2013 at 18:29
  • 1
    normally when you run a script it runs in a subshell. so once it exits all changes are lost. To run it in the current shell run it like . script.sh and it will run in the current shell. does that fix the issue? else as @Useless said, example please Commented Apr 26, 2013 at 18:30
  • @Doon: That fixed it. But I'm confused as to why. I thought the . was a synonym for source the given file. I guess that makes sense. Source the current file which begins with #!/bin/bash so it runs the script. Does that make sense? Commented Apr 26, 2013 at 18:50
  • it does source it. the dot or source operator executes the script in the current shell context. Normally when you use it execute a script it is to pull in config, but it still executes it. Commented Apr 26, 2013 at 18:55
  • 1
    That must be the most convoluted way to say svcid=$SERIAL_NUMBER that I have ever seen. Commented Apr 26, 2013 at 19:18

2 Answers 2

3

Some (but not necessarily all) shell variables are marked for export to the environment. Only these variables are visible in a child process (such as a shell script). For example:

$ x=3         # shell variable
$ export y=5  # shell variable exported to the environment
$ cat example.sh
echo "value of x: $x"
echo "value of y: $y"
$ bash example.sh
example of x:
example of y: 5
Sign up to request clarification or add additional context in comments.

1 Comment

My problem has been solved, so thanks for the response anyways. I did however try what you suggest and the only part that did not appear to work for me was bash example.sh I could be wrong, but doing it this way, causes example.sh to be run in a new shell, which brings up the original problem. I need something in the existing shell to be available in the subordinate shell script, and the only way to do that is to either pass it in as proposed by hd1 below, or source it as suggested by Doon.
0

It's because the shell script runs in a separate process with a separate environment from the caller. You want to use the env VAR=[value] [script-command] to solve this problem. You asked for an example, here you go:

% bash # I use zsh and this is a paste from the terminal
$ echo "echo $VAR" > /tmp/echo.sh
$ sh /tmp/echo.sh

$ env VAR='hello' sh /tmp/echo.sh
hello
$

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.