I have a Bash function that does some string manipulation on its arguments as a whole ("$@"
) by putting it in a local variable, something like this:
my_func() {
local args="$@"
echo "args: <$args>"
}
my_func "$@"
When I run this in Bash, args
contains all of the arguments that were passed:
$ bash foo.sh foo bar baz
args: <foo bar baz>
However, if I run it in Dash, only the first argument is stored:
$ dash test.sh foo bar baz
args: <foo>
Reading the section on local
in the Ubuntu Wiki's "Dash as /bin/sh" page, it seems that Dash is expanding the local args="$@"
line like so:
local args=foo bar baz
and therefore only putting "foo" in args
and declaring bar
and baz
as (local?) variables. In fact, if I add echo "bar: $bar"
to my_func
and run it with an =
in the arguments it seems to confirm that I am adding variables:
$ foo.sh foo bar=baz
args: <foo>
bar: baz
All this to say, is there a way to get the Bash-like behaviour (of $args
containing "foo bar baz") in Dash?
local IFS=" "; local args="$*"
– Jul 25 '19 at 20:22