August Karlstrom
August Karlstrom

Reputation: 11377

Passing parameters with spaces

Why is the output of test foo bar only foo instead of foo bar?

$ cat test
function f
{
    local args=$1

    echo $args
}
f "$@"

$ bash test foo bar
foo

Upvotes: 0

Views: 219

Answers (2)

cdarke
cdarke

Reputation: 44344

To pass the command-line parameters as a single argument to your function you can join them together using "$*" (instead of $@):

f "$*"

The first character of variable IFS is used as the "glue" between each parameter, the default is a single space.

Upvotes: 2

Ivan Klaric
Ivan Klaric

Reputation: 423

because bar is $2. if you want both foo and bar to be passed as a single param, you have to call it like this:

bash test "foo bar"

Upvotes: 1

Related Questions