Differences between revisions 1 and 2
Revision 1 as of 2008-05-14 13:59:11
Size: 1727
Editor: Lhunath
Comment:
Revision 2 as of 2008-07-25 03:19:54
Size: 1725
Editor: a81-84-52-82
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 5: Line 4:
When you call a script from another, the new script inherits the environment of the original script, just like running any other program in UNIX. Explaining what this means is out of the scope of this guide, but for the most part you can consider the environment to be the current working directory and the exported parameters, which you van view using the `export` command. When you call a script from another, the new script inherits the environment of the original script, just like running any other program in UNIX. Explaining what this means is out of the scope of this guide, but for the most part you can consider the environment to be the current working directory and the exported parameters, which you can view using the `export` command.

Anchor(Sourcing)

Sourcing

When you call a script from another, the new script inherits the environment of the original script, just like running any other program in UNIX. Explaining what this means is out of the scope of this guide, but for the most part you can consider the environment to be the current working directory and the exported parameters, which you can view using the export command.

When the script that you ran (or any other program, for that matter) finishes executing, its environment is discarded. The environment of the first script will be same as it was before the second script was called, although of course some of bash's special parameters may have changed (such as the value of $?, the return value of the most recent command). This means, for example, you can't simply run a script to change your current working directory for you.


  • In the FAQ: BR [http://wooledge.org/mywiki/BashFAQ/060 I'm trying to write a script that will change directory (or set a variable), but after the script finishes, I'm back where I started (or my variable isn't set)!]


  • What you can do instead is to source the script instead of running it directly, using the . (dot) command:

. myscript   #runs the commands from the file myscript in this environment

This makes the commands in myscript run similarly to the way they would if they were in a function defined in the original Bash process, and able to change its environment.

Note that Bash has a second name for this command, source, but since this works identically to the . command, it's probably easier to just forget about it and use the . command, as that will work everywhere.

BashGuide/Sourcing (last edited 2014-11-28 08:19:04 by geirha)