Question

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?

Was it helpful?

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

OTHER TIPS

Try doing this instead:

cd .. && cd C

Alternatively, your original command should work if you remove the parentheses:

cd ../ ; cd C

Parentheses cause the command(s) to be executed in a subshell, which is why you're not seeing the directory change. See here for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top