How to pass list of arguments into a function
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
New contributor
add a comment |
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
New contributor
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
3 hours ago
add a comment |
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
New contributor
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
shell-script function arguments
New contributor
New contributor
edited 3 hours ago
CharacterClass
New contributor
asked 4 hours ago
CharacterClassCharacterClass
103
103
New contributor
New contributor
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
3 hours ago
add a comment |
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
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
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
3 hours ago
@WeijunZhou Thanks, this worked!
– CharacterClass
3 hours ago
add a comment |
1 Answer
1
active
oldest
votes
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.)
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
add a comment |
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.
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
Required, but never shown
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
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.)
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
add a comment |
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.)
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
add a comment |
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.)
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.)
edited 3 hours ago
answered 3 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
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
add a comment |
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 hours ago
There are actually two loops, they're nested. Is there a version of the first instance that works?
– CharacterClass
3 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
3 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
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
Thank you. I've updated the question. The loop will serve only to concatenate the variables.
– CharacterClass
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
@CharacterClass, right, that looks simple since you can use multiple brace expansions. Edited, the section in the middle.
– ilkkachu
3 hours ago
add a comment |
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.
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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
3 hours ago