0

I want the user to be able to run a script which looks like this now:

# script.sh
export PASSPHRASE=123

and then be able to do this in the same terminal as that in which the script was evoked:

$ echo $PASSPHRASE
123

In other words, I want the variable in the script to be available to the user after the script has been run. How do I do that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
PetaspeedBeaver
  • 433
  • 6
  • 17
  • 4
    `source /path/to/script.sh` or shorter `. /path/to/script.sh` – Cyrus Jun 12 '16 at 17:08
  • Well, just do it. The environment variables are persistent during a session. All you have to take care of that you do not fork a new shell for the execution of a script, since after leaving that shell at the end of an execution all setting of _that_ shell will be lost again. You can do that by means of the `source` command. So `source ./myscript.sh` will do the trick. – arkascha Jun 12 '16 at 17:09
  • Or `. /path/to/script.sh` (or, if it is in a directory listed on your PATH, then `. script.sh` — and it doesn't even need to be executable to be found and read). The `.` command is the traditional Bourne shell syntax (and is the one supported by POSIX, so it will work with more shells). The `source` command is an import from C shell supported by Bash — in Bash, the two are fully equivalent as far as I know, apart from the spelling. – Jonathan Leffler Jun 12 '16 at 17:10

0 Answers0