Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why commands executed inside backticks doesn't affect the current shell?

Tags:

bash

shell

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?

like image 367
curious Avatar asked Oct 20 '25 04:10

curious


2 Answers

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

like image 91
Nir Alfasi Avatar answered Oct 21 '25 20:10

Nir Alfasi


Subshells have a different $BASHPID's--they're forked 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).

like image 45
PSkocik Avatar answered Oct 21 '25 21:10

PSkocik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!