Accessing array index variable from bash shell script loop?
I want to access the array index variable while looping thru an array in my bash shell script.
myscript.sh
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in ${AR[*]}; do
echo $i
done
The result of the above script is:
foo
bar
baz
bat
The result I seek is:
0
1
2
3
How do I alter my script to achieve this?
bash shell shell-script
add a comment |
I want to access the array index variable while looping thru an array in my bash shell script.
myscript.sh
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in ${AR[*]}; do
echo $i
done
The result of the above script is:
foo
bar
baz
bat
The result I seek is:
0
1
2
3
How do I alter my script to achieve this?
bash shell shell-script
3
Also note that you basically never want"${array[*]}"
instead of"${array[@]}"
. Using*
instead of@
more or less treats it as a string instead of an array.
– jordanm
Apr 23 '16 at 2:40
add a comment |
I want to access the array index variable while looping thru an array in my bash shell script.
myscript.sh
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in ${AR[*]}; do
echo $i
done
The result of the above script is:
foo
bar
baz
bat
The result I seek is:
0
1
2
3
How do I alter my script to achieve this?
bash shell shell-script
I want to access the array index variable while looping thru an array in my bash shell script.
myscript.sh
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in ${AR[*]}; do
echo $i
done
The result of the above script is:
foo
bar
baz
bat
The result I seek is:
0
1
2
3
How do I alter my script to achieve this?
bash shell shell-script
bash shell shell-script
asked Apr 23 '16 at 2:31
MowzerMowzer
179227
179227
3
Also note that you basically never want"${array[*]}"
instead of"${array[@]}"
. Using*
instead of@
more or less treats it as a string instead of an array.
– jordanm
Apr 23 '16 at 2:40
add a comment |
3
Also note that you basically never want"${array[*]}"
instead of"${array[@]}"
. Using*
instead of@
more or less treats it as a string instead of an array.
– jordanm
Apr 23 '16 at 2:40
3
3
Also note that you basically never want
"${array[*]}"
instead of "${array[@]}"
. Using *
instead of @
more or less treats it as a string instead of an array.– jordanm
Apr 23 '16 at 2:40
Also note that you basically never want
"${array[*]}"
instead of "${array[@]}"
. Using *
instead of @
more or less treats it as a string instead of an array.– jordanm
Apr 23 '16 at 2:40
add a comment |
2 Answers
2
active
oldest
votes
You can do this using indirection. From the bash
manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
For your example:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Note that this also work for non-succesive indexes:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
add a comment |
Additional to jordanm's answer you can also do a C
like loop in bash
:
for ((idx=0; idx<${#array[@]}; ++idx)); do
echo "$idx" "${array[idx]}"
done
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
});
}
});
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%2f278502%2faccessing-array-index-variable-from-bash-shell-script-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this using indirection. From the bash
manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
For your example:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Note that this also work for non-succesive indexes:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
add a comment |
You can do this using indirection. From the bash
manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
For your example:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Note that this also work for non-succesive indexes:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
add a comment |
You can do this using indirection. From the bash
manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
For your example:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Note that this also work for non-succesive indexes:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz
You can do this using indirection. From the bash
manpage:
If the first character of parameter is an exclamation point (
!
), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of${!prefix*}
and${!name[@]}
described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
For your example:
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[0]}=foo
${AR[1]}=bar
${AR[2]}=baz
${AR[3]}=bat
Note that this also work for non-succesive indexes:
#!/bin/bash
AR=([3]='foo' [5]='bar' [25]='baz' [7]='bat')
for i in "${!AR[@]}"; do
printf '${AR[%s]}=%sn' "$i" "${AR[i]}"
done
This results in:
${AR[3]}=foo
${AR[5]}=bar
${AR[7]}=bat
${AR[25]}=baz
edited Feb 8 at 14:48
Kusalananda
131k17250409
131k17250409
answered Apr 23 '16 at 2:38
jordanmjordanm
30.8k38695
30.8k38695
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
add a comment |
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
2
2
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
I think in the semicolon was meant to be outside the double quote in: for i in "${!AR[@]};" do
– Alexandros
Mar 17 '18 at 16:05
add a comment |
Additional to jordanm's answer you can also do a C
like loop in bash
:
for ((idx=0; idx<${#array[@]}; ++idx)); do
echo "$idx" "${array[idx]}"
done
add a comment |
Additional to jordanm's answer you can also do a C
like loop in bash
:
for ((idx=0; idx<${#array[@]}; ++idx)); do
echo "$idx" "${array[idx]}"
done
add a comment |
Additional to jordanm's answer you can also do a C
like loop in bash
:
for ((idx=0; idx<${#array[@]}; ++idx)); do
echo "$idx" "${array[idx]}"
done
Additional to jordanm's answer you can also do a C
like loop in bash
:
for ((idx=0; idx<${#array[@]}; ++idx)); do
echo "$idx" "${array[idx]}"
done
answered Apr 23 '16 at 2:41
pfnueselpfnuesel
2,71941939
2,71941939
add a comment |
add a comment |
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%2f278502%2faccessing-array-index-variable-from-bash-shell-script-loop%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
3
Also note that you basically never want
"${array[*]}"
instead of"${array[@]}"
. Using*
instead of@
more or less treats it as a string instead of an array.– jordanm
Apr 23 '16 at 2:40