Can you pass an array to a function, but only make one parameter of the function receive the entire array?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
If I passed an array to a function:
func()
echo $1
echo $2
echo $3
myArray=("This" "Is" "An Array")
func "$myArray[@]"
Then each element of the array will be passed to a separate parameter of the function.
Is there a way to only make one parameter of the function receive the entire array?
linux bash
add a comment |Â
up vote
2
down vote
favorite
If I passed an array to a function:
func()
echo $1
echo $2
echo $3
myArray=("This" "Is" "An Array")
func "$myArray[@]"
Then each element of the array will be passed to a separate parameter of the function.
Is there a way to only make one parameter of the function receive the entire array?
linux bash
1
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.mystring="$*"
insidefunc()
.
â cas
Jan 22 at 9:44
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
If I passed an array to a function:
func()
echo $1
echo $2
echo $3
myArray=("This" "Is" "An Array")
func "$myArray[@]"
Then each element of the array will be passed to a separate parameter of the function.
Is there a way to only make one parameter of the function receive the entire array?
linux bash
If I passed an array to a function:
func()
echo $1
echo $2
echo $3
myArray=("This" "Is" "An Array")
func "$myArray[@]"
Then each element of the array will be passed to a separate parameter of the function.
Is there a way to only make one parameter of the function receive the entire array?
linux bash
asked Jan 22 at 9:33
John
1124
1124
1
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.mystring="$*"
insidefunc()
.
â cas
Jan 22 at 9:44
add a comment |Â
1
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.mystring="$*"
insidefunc()
.
â cas
Jan 22 at 9:44
1
1
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.
mystring="$*"
inside func()
.â cas
Jan 22 at 9:44
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.
mystring="$*"
inside func()
.â cas
Jan 22 at 9:44
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
You can, sort of. Bash and ksh93 have name references, which are somewhat like pointers and allow you to pass the array name to the function, and use it from there, say:
#!/bin/bash
function byname
typeset -n _p=$1
echo "second item of '$1' is $_p[1]"
echo "second arg to this function is "$2""
blah=(a b c)
byname blah "other arg"
Though in Bash, the name of the nameref (_p
here) must be different from the name of the variable it points to, so it's not very usable with recursive functions. In ksh it works with the same name only in ksh style functions (function foo
instead of foo()
).
As the label says, that's a reference, not a copy, so if you modify the array in the function, the changes show in the main program.
The other, worse, alternative is to concatenate the array to a string, and pass it as a variable:
function concated
echo "the whole array is "$1""
concated "$blah[*]"
But that pretty much defeats the point of using an array in the first place, unless you come up with some elaborate system for array to string packing.
The above calls of course print:
second item of 'blah' is b
second arg to this function is "other arg"
the whole array is "a b c"
1
Another alternative is to call the function asfunc "$#array[@]" "$array[@]"
and dolocal array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arrays
â Stéphane Chazelas
Jan 22 at 12:04
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can, sort of. Bash and ksh93 have name references, which are somewhat like pointers and allow you to pass the array name to the function, and use it from there, say:
#!/bin/bash
function byname
typeset -n _p=$1
echo "second item of '$1' is $_p[1]"
echo "second arg to this function is "$2""
blah=(a b c)
byname blah "other arg"
Though in Bash, the name of the nameref (_p
here) must be different from the name of the variable it points to, so it's not very usable with recursive functions. In ksh it works with the same name only in ksh style functions (function foo
instead of foo()
).
As the label says, that's a reference, not a copy, so if you modify the array in the function, the changes show in the main program.
The other, worse, alternative is to concatenate the array to a string, and pass it as a variable:
function concated
echo "the whole array is "$1""
concated "$blah[*]"
But that pretty much defeats the point of using an array in the first place, unless you come up with some elaborate system for array to string packing.
The above calls of course print:
second item of 'blah' is b
second arg to this function is "other arg"
the whole array is "a b c"
1
Another alternative is to call the function asfunc "$#array[@]" "$array[@]"
and dolocal array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arrays
â Stéphane Chazelas
Jan 22 at 12:04
add a comment |Â
up vote
2
down vote
You can, sort of. Bash and ksh93 have name references, which are somewhat like pointers and allow you to pass the array name to the function, and use it from there, say:
#!/bin/bash
function byname
typeset -n _p=$1
echo "second item of '$1' is $_p[1]"
echo "second arg to this function is "$2""
blah=(a b c)
byname blah "other arg"
Though in Bash, the name of the nameref (_p
here) must be different from the name of the variable it points to, so it's not very usable with recursive functions. In ksh it works with the same name only in ksh style functions (function foo
instead of foo()
).
As the label says, that's a reference, not a copy, so if you modify the array in the function, the changes show in the main program.
The other, worse, alternative is to concatenate the array to a string, and pass it as a variable:
function concated
echo "the whole array is "$1""
concated "$blah[*]"
But that pretty much defeats the point of using an array in the first place, unless you come up with some elaborate system for array to string packing.
The above calls of course print:
second item of 'blah' is b
second arg to this function is "other arg"
the whole array is "a b c"
1
Another alternative is to call the function asfunc "$#array[@]" "$array[@]"
and dolocal array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arrays
â Stéphane Chazelas
Jan 22 at 12:04
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can, sort of. Bash and ksh93 have name references, which are somewhat like pointers and allow you to pass the array name to the function, and use it from there, say:
#!/bin/bash
function byname
typeset -n _p=$1
echo "second item of '$1' is $_p[1]"
echo "second arg to this function is "$2""
blah=(a b c)
byname blah "other arg"
Though in Bash, the name of the nameref (_p
here) must be different from the name of the variable it points to, so it's not very usable with recursive functions. In ksh it works with the same name only in ksh style functions (function foo
instead of foo()
).
As the label says, that's a reference, not a copy, so if you modify the array in the function, the changes show in the main program.
The other, worse, alternative is to concatenate the array to a string, and pass it as a variable:
function concated
echo "the whole array is "$1""
concated "$blah[*]"
But that pretty much defeats the point of using an array in the first place, unless you come up with some elaborate system for array to string packing.
The above calls of course print:
second item of 'blah' is b
second arg to this function is "other arg"
the whole array is "a b c"
You can, sort of. Bash and ksh93 have name references, which are somewhat like pointers and allow you to pass the array name to the function, and use it from there, say:
#!/bin/bash
function byname
typeset -n _p=$1
echo "second item of '$1' is $_p[1]"
echo "second arg to this function is "$2""
blah=(a b c)
byname blah "other arg"
Though in Bash, the name of the nameref (_p
here) must be different from the name of the variable it points to, so it's not very usable with recursive functions. In ksh it works with the same name only in ksh style functions (function foo
instead of foo()
).
As the label says, that's a reference, not a copy, so if you modify the array in the function, the changes show in the main program.
The other, worse, alternative is to concatenate the array to a string, and pass it as a variable:
function concated
echo "the whole array is "$1""
concated "$blah[*]"
But that pretty much defeats the point of using an array in the first place, unless you come up with some elaborate system for array to string packing.
The above calls of course print:
second item of 'blah' is b
second arg to this function is "other arg"
the whole array is "a b c"
edited Jan 22 at 12:05
Stéphane Chazelas
281k53518849
281k53518849
answered Jan 22 at 11:45
ilkkachu
49.8k674137
49.8k674137
1
Another alternative is to call the function asfunc "$#array[@]" "$array[@]"
and dolocal array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arrays
â Stéphane Chazelas
Jan 22 at 12:04
add a comment |Â
1
Another alternative is to call the function asfunc "$#array[@]" "$array[@]"
and dolocal array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arrays
â Stéphane Chazelas
Jan 22 at 12:04
1
1
Another alternative is to call the function as
func "$#array[@]" "$array[@]"
and do local array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arraysâ Stéphane Chazelas
Jan 22 at 12:04
Another alternative is to call the function as
func "$#array[@]" "$array[@]"
and do local array=("$@:2:$1"); shift "$(($1 + 1))"
in the function, though that wouldn't preserve the indicies for sparse arraysâ Stéphane Chazelas
Jan 22 at 12:04
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418800%2fcan-you-pass-an-array-to-a-function-but-only-make-one-parameter-of-the-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
yes, same as with args to the script itself, you can use "$@" to get each arg as a separate positional parameter ($1, $2, $3, etc) or "$*" to get all args as one space-separated (with default $IFS) string. e.g.
mystring="$*"
insidefunc()
.â cas
Jan 22 at 9:44