2

I tried cd . ~ in terminal and I saw the same result as when I type cd ~ .

I want to know how this is done. Or better to say what is the priority of the arguments which comes after a command?

1
  • What was the same result. You would only get the same result when in your home directory. Try the same example from another directory.
    – Dan
    Apr 2, 2014 at 12:25

2 Answers 2

5

You saw the same result because I'm sure that you test those commands from your home directory which is /home/your_username equivalent with ~ - see Tilde Expansion, or $HOME environment variable. To test properly, you should use:

cd / ; cd . ~

and:

cd / ; cd ~ .

In general if you use:

cd first_directory second_directory

you will change the shell working directory to first_directory, not to second_directory, so the second argument in the cd command is ignored (see also help cd to understand better).

And the . (dot) in this case is equivalent with the path of the shell current working directory which is given by pwd command (see Commandline shortcut for current directory similar to ~ for home directory?).

So cd . ~ is equivalent with cd . which is equivalent with cd $(pwd) and cd ~ . is equivalent with cd ~ which is equivalent with cd $HOME.

2
  • cd is a shell builtin, you'll find the corresponding man page in man bash if you're using bash of course Apr 2, 2014 at 12:44
  • @SylvainPineau Sorry, I use this function. I updated my answer. Apr 2, 2014 at 12:46
2

It gives the same result because you're running them from your home directory. Try moving to /tmp to observe a different behaviour.

Only the first positional parameter of the cd command is taken into account:

cd: usage: cd [-L|[-P [-e]]] [dir]
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .