Let's take the two lines below which give us two different results.p=$(cd ~ && pwd) ; echo $pp=$(cd ~ |  pwd) ; echo $pHow do the two differ?
What is the difference between && and | in bash script?
bash
In p=$(cd ~ && pwd):The command substitution, $(), runs in a subshellcd ~ changes directory to ~ (your home), if cd succeeds (&&) then pwd prints the directory name on STDOUT, hence the string saved on p will be your home directory e.g. /home/foobarIn p=$(cd ~ |  pwd):Again $() spawns a subshellThe commands on both sides of | run in respective subshells (and both starts off at the same time)so cd ~ is done in a subshell, and pwd in a separate subshellso you would get only the STDOUT from pwd i.e. from where you run the command, this can be any directory as you can imagine, hence p will contain the directory name from where the command is invoked, not your home directory