Elsewhere I have seen a cd function as below:

 builtin cd "$@"

why is it recommended to use $@ instead of $1?

I created a test directory "r st" and called the script containing this function and it worked either way

$ . cdtest.sh "r st"

but $ . cdtest.sh r st failed whether I used "$@" or "$1"

  • 1
    cd "$*" also wont work correctly with more than 1 arg. Commented Oct 25, 2017 at 20:42

4 Answers 4


Because, according to bash(1), cd takes arguments

   cd [-L|[-P [-e]] [-@]] [dir]
          Change  the  current  directory to dir.  if dir is not supplied,

so therefore the directory actually may not be in $1 as that could instead be an option such as -L or another flag.

How bad is this?

$ cd -L /var/tmp
$ pwd
$ cd() { builtin cd "$1"; }
$ cd -L /var/tmp
$ pwd

Things could go very awry if you end up not where you expect using cd "$1"


Using "$@" will pass all arguments to cd where as $1 will only pass the first argument.

In your examples

$ . cdtest.sh "r st"

always works as you only pass in one argument, but if you were to pass in a flag as well such as

$ . cdtest.sh -L "r st"

Then only "$@" will execute correctly where "$1" will expand to cd -L losing the directory entirely.


$ . cdtest.sh r st

Fails in both cases as you are passing two parameters to cd, r and st which is not a valid way to execute cd. Parameters are separated by spaces which must be quoted (as in your first example) or escaped (r\ st) to be treated as one argument.

In the case of cd however it is very uncommon to pass in flags and you cannot pass in multiple directories so you will not see the difference in a real world use of either "$1" or "$@" for cd. But for other commands you will notice a difference so it is best practice to always use "$@" when you want to create a wrapper function or script like this.


There's also the case when there are no arguments:

$ cd /tmp; cd; pwd
$ cd_func() { builtin cd "$1"; }
$ cd_func /tmp; cd_func; pwd

cd without any arguments changes to the home directory. Without any arguments, "$@" expands to nothing, but "$1" expands to the empty string. These are different:

$ args() { for i in "$@"; do echo "|$i|"; done; }
$ args
$ args ""
  • Somewhat unrelated, but: I finally understand why a null argument to cd is a good thing. It causes an error instead of changing to the home directory. With some other commands, notably rsync, a null first argument causes unexpected behavior (includes the current directory in the sources of the transfer).
    – Joe
    Commented Oct 27, 2017 at 20:23

Arguments to a bash script are space delimited. $1 is the first argument. In your examples...

In example 1, $1 is the string "r st" ... in the second example, $1 is the one character string 'r' ...

$@ is all the arguments.

You must log in to answer this question.

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