Sample Header Ad - 728x90

Putting "$@" in a local variable in dash

6 votes
1 answer
1175 views
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: "
}

my_func "$@"
When I run this in Bash, args contains all of the arguments that were passed:
$ bash foo.sh foo bar baz
args:
However, if I run it in Dash, only the first argument is stored:
$ dash test.sh foo bar baz
args:
Reading [the section on local in the Ubuntu Wiki's "Dash as /bin/sh" page](https://wiki.ubuntu.com/DashAsBinSh#local) , 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: 
bar: baz
All this to say, is there a way to get the Bash-like behaviour (of $args containing "foo bar baz") in Dash?
Asked by Harry Cutts (242 rep)
Jul 25, 2019, 08:17 PM
Last activity: Feb 15, 2024, 03:41 PM