How to pass list of arguments into a function












0















I want to pass arguments from a loop into a function func. For simplicity let's say we are working with the loop



for x in {1..5}
do
for y in {a..c}
do
echo $x$y
done
done


I'm just echoing because I don't know what to do. I'd like to run the equivalent of func 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c.



How do I do this?










share|improve this question









New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

    – Weijun Zhou
    3 hours ago













  • @WeijunZhou Thanks, this worked!

    – CharacterClass
    2 hours ago
















0















I want to pass arguments from a loop into a function func. For simplicity let's say we are working with the loop



for x in {1..5}
do
for y in {a..c}
do
echo $x$y
done
done


I'm just echoing because I don't know what to do. I'd like to run the equivalent of func 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c.



How do I do this?










share|improve this question









New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

    – Weijun Zhou
    3 hours ago













  • @WeijunZhou Thanks, this worked!

    – CharacterClass
    2 hours ago














0












0








0








I want to pass arguments from a loop into a function func. For simplicity let's say we are working with the loop



for x in {1..5}
do
for y in {a..c}
do
echo $x$y
done
done


I'm just echoing because I don't know what to do. I'd like to run the equivalent of func 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c.



How do I do this?










share|improve this question









New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I want to pass arguments from a loop into a function func. For simplicity let's say we are working with the loop



for x in {1..5}
do
for y in {a..c}
do
echo $x$y
done
done


I'm just echoing because I don't know what to do. I'd like to run the equivalent of func 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c.



How do I do this?







shell-script function arguments






share|improve this question









New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago







CharacterClass













New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









CharacterClassCharacterClass

103




103




New contributor




CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






CharacterClass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

    – Weijun Zhou
    3 hours ago













  • @WeijunZhou Thanks, this worked!

    – CharacterClass
    2 hours ago



















  • In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

    – Weijun Zhou
    3 hours ago













  • @WeijunZhou Thanks, this worked!

    – CharacterClass
    2 hours ago

















In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

– Weijun Zhou
3 hours ago







In the loop: y="$y $x"; outside the loop: func $y. This only covers the most simple case. If you have special characters in your list, you need to do something more complex.

– Weijun Zhou
3 hours ago















@WeijunZhou Thanks, this worked!

– CharacterClass
2 hours ago





@WeijunZhou Thanks, this worked!

– CharacterClass
2 hours ago










1 Answer
1






active

oldest

votes


















1














func {1..5} would be equivalent to func 1 2 3 4 5.



In general, the list of words in a for statement is just like any list of words in a command, so you can just replace the loop with a single invocation of the command, with whatever list you used there moved to the command arguments.



Also, you can use multiple brace expansions together: {1..5}{a..c} would create the list 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c (as distinct words), so in the case you show, func {1..5}{a..c} should work.





If your loop does something more complex to create the arguments to the final command, you can use an array to collect them (in Bash/ksh/zsh). Assuming we have generate_arg that has to be run to produce the arguments to func:



args=()
for i in {1..5}; do
args+=( "$(generate_arg "$i")" )
done
func "${args[@]}"


(Using an array is better than concatenating the values to a string in that it keeps values with whitespace intact.)






share|improve this answer


























  • There are actually two loops, they're nested. Is there a version of the first instance that works?

    – CharacterClass
    2 hours ago











  • @CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

    – ilkkachu
    2 hours ago











  • Thank you. I've updated the question. The loop will serve only to concatenate the variables.

    – CharacterClass
    2 hours ago











  • @CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

    – ilkkachu
    2 hours ago











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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






CharacterClass is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505748%2fhow-to-pass-list-of-arguments-into-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














func {1..5} would be equivalent to func 1 2 3 4 5.



In general, the list of words in a for statement is just like any list of words in a command, so you can just replace the loop with a single invocation of the command, with whatever list you used there moved to the command arguments.



Also, you can use multiple brace expansions together: {1..5}{a..c} would create the list 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c (as distinct words), so in the case you show, func {1..5}{a..c} should work.





If your loop does something more complex to create the arguments to the final command, you can use an array to collect them (in Bash/ksh/zsh). Assuming we have generate_arg that has to be run to produce the arguments to func:



args=()
for i in {1..5}; do
args+=( "$(generate_arg "$i")" )
done
func "${args[@]}"


(Using an array is better than concatenating the values to a string in that it keeps values with whitespace intact.)






share|improve this answer


























  • There are actually two loops, they're nested. Is there a version of the first instance that works?

    – CharacterClass
    2 hours ago











  • @CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

    – ilkkachu
    2 hours ago











  • Thank you. I've updated the question. The loop will serve only to concatenate the variables.

    – CharacterClass
    2 hours ago











  • @CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

    – ilkkachu
    2 hours ago
















1














func {1..5} would be equivalent to func 1 2 3 4 5.



In general, the list of words in a for statement is just like any list of words in a command, so you can just replace the loop with a single invocation of the command, with whatever list you used there moved to the command arguments.



Also, you can use multiple brace expansions together: {1..5}{a..c} would create the list 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c (as distinct words), so in the case you show, func {1..5}{a..c} should work.





If your loop does something more complex to create the arguments to the final command, you can use an array to collect them (in Bash/ksh/zsh). Assuming we have generate_arg that has to be run to produce the arguments to func:



args=()
for i in {1..5}; do
args+=( "$(generate_arg "$i")" )
done
func "${args[@]}"


(Using an array is better than concatenating the values to a string in that it keeps values with whitespace intact.)






share|improve this answer


























  • There are actually two loops, they're nested. Is there a version of the first instance that works?

    – CharacterClass
    2 hours ago











  • @CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

    – ilkkachu
    2 hours ago











  • Thank you. I've updated the question. The loop will serve only to concatenate the variables.

    – CharacterClass
    2 hours ago











  • @CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

    – ilkkachu
    2 hours ago














1












1








1







func {1..5} would be equivalent to func 1 2 3 4 5.



In general, the list of words in a for statement is just like any list of words in a command, so you can just replace the loop with a single invocation of the command, with whatever list you used there moved to the command arguments.



Also, you can use multiple brace expansions together: {1..5}{a..c} would create the list 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c (as distinct words), so in the case you show, func {1..5}{a..c} should work.





If your loop does something more complex to create the arguments to the final command, you can use an array to collect them (in Bash/ksh/zsh). Assuming we have generate_arg that has to be run to produce the arguments to func:



args=()
for i in {1..5}; do
args+=( "$(generate_arg "$i")" )
done
func "${args[@]}"


(Using an array is better than concatenating the values to a string in that it keeps values with whitespace intact.)






share|improve this answer















func {1..5} would be equivalent to func 1 2 3 4 5.



In general, the list of words in a for statement is just like any list of words in a command, so you can just replace the loop with a single invocation of the command, with whatever list you used there moved to the command arguments.



Also, you can use multiple brace expansions together: {1..5}{a..c} would create the list 1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c (as distinct words), so in the case you show, func {1..5}{a..c} should work.





If your loop does something more complex to create the arguments to the final command, you can use an array to collect them (in Bash/ksh/zsh). Assuming we have generate_arg that has to be run to produce the arguments to func:



args=()
for i in {1..5}; do
args+=( "$(generate_arg "$i")" )
done
func "${args[@]}"


(Using an array is better than concatenating the values to a string in that it keeps values with whitespace intact.)







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









ilkkachuilkkachu

60.9k1098174




60.9k1098174













  • There are actually two loops, they're nested. Is there a version of the first instance that works?

    – CharacterClass
    2 hours ago











  • @CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

    – ilkkachu
    2 hours ago











  • Thank you. I've updated the question. The loop will serve only to concatenate the variables.

    – CharacterClass
    2 hours ago











  • @CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

    – ilkkachu
    2 hours ago



















  • There are actually two loops, they're nested. Is there a version of the first instance that works?

    – CharacterClass
    2 hours ago











  • @CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

    – ilkkachu
    2 hours ago











  • Thank you. I've updated the question. The loop will serve only to concatenate the variables.

    – CharacterClass
    2 hours ago











  • @CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

    – ilkkachu
    2 hours ago

















There are actually two loops, they're nested. Is there a version of the first instance that works?

– CharacterClass
2 hours ago





There are actually two loops, they're nested. Is there a version of the first instance that works?

– CharacterClass
2 hours ago













@CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

– ilkkachu
2 hours ago





@CharacterClass, well, now I'm not exactly sure what your situation is then. Can you edit your post to show those nested loops in a bit more detail?

– ilkkachu
2 hours ago













Thank you. I've updated the question. The loop will serve only to concatenate the variables.

– CharacterClass
2 hours ago





Thank you. I've updated the question. The loop will serve only to concatenate the variables.

– CharacterClass
2 hours ago













@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

– ilkkachu
2 hours ago





@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.

– ilkkachu
2 hours ago










CharacterClass is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















CharacterClass is a new contributor. Be nice, and check out our Code of Conduct.













CharacterClass is a new contributor. Be nice, and check out our Code of Conduct.












CharacterClass is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505748%2fhow-to-pass-list-of-arguments-into-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

CARDNET

Boot-repair Failure: Unable to locate package grub-common:i386

Aws NAT - Aws IGW- Aws router