Toran Billups
Toran Billups

Reputation: 27399

What does $@ mean in a bash script?

I'm reading through my .bashrc and noticed the following

cd "$@" && some_other_function

What does the $@ mean exactly?

Upvotes: 0

Views: 518

Answers (5)

user2659589
user2659589

Reputation:

When $@ is empty, $@ doesn't expand to an empty string; it is removed altogether. So your test is not:

[ -n "" ]

but rather:

[ -n ]

Now -n isn't an operator, but just a non-empty string, which always tests as true.

Upvotes: 0

jahroy
jahroy

Reputation: 22692

$@ represents all of the arguments passed to the script.

If you call a script named foo.sh like this:

$>  foo.sh boo goo loo

The output of $@ will be this:

 > boo goo loo

Upvotes: 1

geekosaur
geekosaur

Reputation: 61369

"$@" expands to a list of quoted command line parameters. It is subtly different from "$*": given

set "a b" c d

"$*" expands to

"a b c d"

whereas "$@" expands to

"a b" c d

and $* (or $@) expands to

a b c d

that is, "$*" produces a single string but "$@" replicates the original quoting , $* loses the quoting.

Upvotes: 5

Ry-
Ry-

Reputation: 224855

It's all of the positional parameters, each double-quoted. http://tldp.org/LDP/abs/html/internalvariables.html#APPREF

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 992747

From man bash:

@
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.

Upvotes: 1

Related Questions