Size: 2536
Comment: explain the forking a bit better. many newbies read the faq.
|
Size: 2876
Comment: consistent quoting
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[Anchor(faq60)]] | <<Anchor(faq60)>> |
Line 3: | Line 3: |
Line 5: | Line 4: |
Line 11: | Line 9: |
If one executes this simple script, what happens? Bash forks, resulting in a parent (which is the bash in which you ran the command that executed the script) and a child (which is the bash fork that runs the actual script code). The child is started and the parent waits for it to finish. The child executes the script, including the {{{chdir(2)}}} system call, changes its current directory to `/tmp`, and then exits. The parent, which was waiting for the child, harvests the child's exit status (presumably 0 for success), and then bash carries on with the next command. Nowhere in this process has the parent's current working directory changed, only the child's (which died as the script reached its end). | If one executes this simple script, what happens? Bash forks, resulting in a parent (the interactive shell in which you typed the command) and a child (a new shell that reads and executes the script). The child runs, while the parent waits for it to finish. The child reads and executes the script, changes its current directory to `/tmp`, and then exits. The parent, which was waiting for the child, harvests the child's exit status (presumably 0 for success), and then carries on with the next command. Nowhere in this process has the parent's current working directory changed -- only the child's. |
Line 13: | Line 11: |
Moreover, there is '''no conceivable way''' you can ''ever'' have a child process affect ''any'' part of the parent's environment, which includes its variables as well as its current working directory. | A child process can never affect ''any'' part of the parent's environment, which includes its variables, its current working directory, its open files, its resource limits, etc. |
Line 15: | Line 13: |
So, how does one go about changing the current working directory of the ''parent''? You can still have the {{{cd}}} command in an external file, but you can't ''run it'' as a script. That would cause the forking explained earlier. Instead, you must source it with {{{.}}} (or the Bash-only synonym, {{{source}}}). Sourcing basically means you execute the commands in a file using the ''current'' shell; not in a forked shell (child shell): | So, how does one go about changing the current working directory of the ''parent''? You can still have the {{{cd}}} command in an external file, but you can't ''run it'' as a script. That would cause the forking explained earlier. Instead, you must source it with {{{.}}} (or the Bash-only synonym, {{{source}}}). Sourcing basically means you execute the commands in a file using the ''current'' shell, not in a forked shell (child shell): |
Line 18: | Line 16: |
. $HOME/mycd # Source that file, executing the 'cd /tmp' command in the current shell. | . "$HOME/mycd" # Source that file, executing the 'cd /tmp' command in the current shell. |
Line 22: | Line 20: |
The same thing applies to setting variables. {{{.}}} ('dot') the file that contains the commands; don't try to run it. | The same thing applies to setting variables. {{{.}}} ("dot in") the file that contains the commands; don't try to run it. |
Line 24: | Line 22: |
If the command you execute is a function, not a script, it will be executed in the current shell. Therefore, it's possible to define a function to what we tried to do with an external file in the examples above, without needing to 'dot' or 'source' anything. Define the following function and then call it simply by executing `mycd`: | If the command you execute is a function, not a script, it will be executed in the current shell. Therefore, it's possible to define a function to do what we tried to do with an external file in the examples above, without needing to "dot in" or "source" anything. Define the following function and then call it simply by typing `mycd`: |
Line 29: | Line 27: |
Put it in `~/.bashrc` or similar if you want the function to automatically be available in every '''new session''' you open '''after''' putting it in that file. | Put it in `~/.bashrc` [[DotFiles|or similar]] if you want the function to be available automatically in every new shell you open. Some people prefer to use [[BashFAQ/080|aliases]] instead of functions. Functions are more powerful, more general, more flexible, and... some people just don't seem to like them. {{{ alias mycd='cd /tmp' # Equivalent to the function shown above. }}} {{{ alias cdlstmp='cd /tmp && ls tmp*' # will take you to /tmp and show you what files there are starting with "tmp" }}} {{{ cdls() { cd "$1" && ls; } # cannot be done with an alias # usage 'cdls directory' }}} |
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)!
Consider this:
#!/bin/sh cd /tmp
If one executes this simple script, what happens? Bash forks, resulting in a parent (the interactive shell in which you typed the command) and a child (a new shell that reads and executes the script). The child runs, while the parent waits for it to finish. The child reads and executes the script, changes its current directory to /tmp, and then exits. The parent, which was waiting for the child, harvests the child's exit status (presumably 0 for success), and then carries on with the next command. Nowhere in this process has the parent's current working directory changed -- only the child's.
A child process can never affect any part of the parent's environment, which includes its variables, its current working directory, its open files, its resource limits, etc.
So, how does one go about changing the current working directory of the parent? You can still have the cd command in an external file, but you can't run it as a script. That would cause the forking explained earlier. Instead, you must source it with . (or the Bash-only synonym, source). Sourcing basically means you execute the commands in a file using the current shell, not in a forked shell (child shell):
echo 'cd /tmp' > "$HOME/mycd" # Create a file that contains the 'cd /tmp' command. . "$HOME/mycd" # Source that file, executing the 'cd /tmp' command in the current shell. pwd # Now, we're in /tmp
The same thing applies to setting variables. . ("dot in") the file that contains the commands; don't try to run it.
If the command you execute is a function, not a script, it will be executed in the current shell. Therefore, it's possible to define a function to do what we tried to do with an external file in the examples above, without needing to "dot in" or "source" anything. Define the following function and then call it simply by typing mycd:
mycd() { cd /tmp; }
Put it in ~/.bashrc or similar if you want the function to be available automatically in every new shell you open.
Some people prefer to use aliases instead of functions. Functions are more powerful, more general, more flexible, and... some people just don't seem to like them.
alias mycd='cd /tmp' # Equivalent to the function shown above.
alias cdlstmp='cd /tmp && ls tmp*' # will take you to /tmp and show you what files there are starting with "tmp"
cdls() { cd "$1" && ls; } # cannot be done with an alias # usage 'cdls directory'