Search code examples
unixcdchain

Unix: chaining commands


I have learned that ';' is equivalent to 'enter' in the command line. But I have this situation.

My directory has the following configuration: Directory A contains directories B and C. In currently in directory B. I do the following:

cd ../

that takes me to A, of course. Then I do:

cd C

that takes me to C.

However, if I chain the two commands, starting in B again:

(cd ../ ; cd C)

I still stay in B. What is going on? Why does it not take me to C?


Solution

  • The process that you start when you execute ( cd ../; cd C) has its own environment, and therefore its own current working directory. This is demonstrable below:

    dogface@computer ~/A/B
    $ ( cd .. ; pwd ; cd C; pwd )
    /home/dogface/A
    /home/dogface/A/C
    
    dogface@computer ~/A/B
    $ pwd
    /home/dogface/A/B
    

    Now try without the '(' and ')' ...

    dogface@computer ~/A/B
    $ cd .. ; cd C
    
    dogface@computer ~/A/C
    $ pwd
    /home/dogface/A/C