I know that backticks are used for command substitution, but why doesn't those commands affect the current shell. For example, executing the following command changes my current working directory to the previous directory I was in.
$> cd -
On the other hand, the following command does show me contents of my previous working directory but doesn't change it.
$> ls `cd -`
Similarly, there could be other examples like
$>ls `PATH=""`
Here, it does shows the contents of my working directory but the PATH
variable doesn't get changed.
Is the command / expression inside backticks executed in a sub-shell?
It works like eval(exp)
in some of the programming languages:
it is evaluated in an external scope, which is a "child" of the current scope (also called subshell environment) - so it "knows" all the vars in the current env, and returns only the result - hence, can't cause any side-effect to the current environment.
Backticks is an old-style and $()
should be used instead. To read more info about command substitution - see the docs
Subshells have a different $BASHPID
's--they're fork
ed off the original shell.
$ echo $BASHPID; echo "`echo $BASHPID`"; echo $BASHPID
When you fork on Unix, the child process copy-on-write inherits (or just inherits by eager copying) data from the parent process. The child process (the subshell) can read all the variables, but writing to any of those will create a private copy for the child (or it already has its own copy if the kernel doesn't do the copy-on-write thing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With