1

I have a script "set_var.sh" written like this

#!/bin/bash

export NAME=release
export ROOT=/Volumes/name/dev/release

but if I run this set_var.sh from terminal, afterward I issue set command to check variables I could not find NAME and ROOT var be set.

I am wondering what is wrong in my case.

user454083
  • 1,337
  • 3
  • 16
  • 31
  • When you run it, all variables get defined within the scope of the execution. If you want these variables to remain in your session, you have to `source` instead. – fedorqui May 20 '14 at 11:53

2 Answers2

0

it was set in sub-shell.

you need

source set_var.sh
Kent
  • 189,393
  • 32
  • 233
  • 301
0

If you simply run set_var.sh, it runs in its own shell which exits, losing the variables that were set.

If you want to change variables in your interactive shell, you can use:

source set_var.sh

or the shorthand,

. set_var.sh

This will execute the lines of the script as if they were typed into your interactive shell.

Note that when you "source" a file this way, it does not require the "shebang" on the first line.

Note also that this is feature exists in Bourne shell as well, but only in the short-form version.

ghoti
  • 45,319
  • 8
  • 65
  • 104