Can you pass an array to a function, but only make one parameter of the function receive the entire array?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question
















  • 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















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?







share|improve this question
















  • 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













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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










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="$*" inside func().
    – cas
    Jan 22 at 9:44













  • 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








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











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"





share|improve this answer


















  • 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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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"





share|improve this answer


















  • 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















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"





share|improve this answer


















  • 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













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"





share|improve this answer














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"






share|improve this answer














share|improve this answer



share|improve this answer








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 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













  • 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








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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay