Printing a Variable Which Contains $ Sign
I have a bash script which contains awscli as well. I am trying to print a variable which is created in a for loop. The variable that I am trying to print contains $ sign because of for loop. I couldn't print the value. Below I am sharing the script. The output of this script is only numbers which is generated in the for loop. I want to print the value which is generated in the command.
#!/bin/bash
declare -i counter=11
declare -i counter2=14
for i in {1..2}
do
declare v1$i=$(aws iam get-group --group-name VideoEditors | awk -v counter1=$counter 'NR==counter1' | awk -F" '{print $4}')
counter=$counter+7
declare v2$i=$(aws iam get-group --group-name VideoEditors | awk -v counter3=$counter2 'NR==counter3' | awk -F" '{print $4}')
counter2=$counter2+7
echo $v1$i
echo $v2$i
done
bash shell-script variable
|
show 2 more comments
I have a bash script which contains awscli as well. I am trying to print a variable which is created in a for loop. The variable that I am trying to print contains $ sign because of for loop. I couldn't print the value. Below I am sharing the script. The output of this script is only numbers which is generated in the for loop. I want to print the value which is generated in the command.
#!/bin/bash
declare -i counter=11
declare -i counter2=14
for i in {1..2}
do
declare v1$i=$(aws iam get-group --group-name VideoEditors | awk -v counter1=$counter 'NR==counter1' | awk -F" '{print $4}')
counter=$counter+7
declare v2$i=$(aws iam get-group --group-name VideoEditors | awk -v counter3=$counter2 'NR==counter3' | awk -F" '{print $4}')
counter2=$counter2+7
echo $v1$i
echo $v2$i
done
bash shell-script variable
1
Use"${var}"instead of just$var;${x}${y}instead of$x$y. See unix.stackexchange.com/questions/4899/…
– Kenneth B. Jensen
Feb 8 at 6:57
2
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
@MichaelHomer I've performed several more tests; turns out that I was printing just$y, which was identical to$x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.
– Kenneth B. Jensen
Feb 8 at 7:20
1
@Kusalananda, the addition works since they're declared integer variables. Evencounter+=7would do. Though I'm not sure if it would be more readable to just use$(( .. ))with regular variables ...
– ilkkachu
Feb 8 at 8:52
|
show 2 more comments
I have a bash script which contains awscli as well. I am trying to print a variable which is created in a for loop. The variable that I am trying to print contains $ sign because of for loop. I couldn't print the value. Below I am sharing the script. The output of this script is only numbers which is generated in the for loop. I want to print the value which is generated in the command.
#!/bin/bash
declare -i counter=11
declare -i counter2=14
for i in {1..2}
do
declare v1$i=$(aws iam get-group --group-name VideoEditors | awk -v counter1=$counter 'NR==counter1' | awk -F" '{print $4}')
counter=$counter+7
declare v2$i=$(aws iam get-group --group-name VideoEditors | awk -v counter3=$counter2 'NR==counter3' | awk -F" '{print $4}')
counter2=$counter2+7
echo $v1$i
echo $v2$i
done
bash shell-script variable
I have a bash script which contains awscli as well. I am trying to print a variable which is created in a for loop. The variable that I am trying to print contains $ sign because of for loop. I couldn't print the value. Below I am sharing the script. The output of this script is only numbers which is generated in the for loop. I want to print the value which is generated in the command.
#!/bin/bash
declare -i counter=11
declare -i counter2=14
for i in {1..2}
do
declare v1$i=$(aws iam get-group --group-name VideoEditors | awk -v counter1=$counter 'NR==counter1' | awk -F" '{print $4}')
counter=$counter+7
declare v2$i=$(aws iam get-group --group-name VideoEditors | awk -v counter3=$counter2 'NR==counter3' | awk -F" '{print $4}')
counter2=$counter2+7
echo $v1$i
echo $v2$i
done
bash shell-script variable
bash shell-script variable
edited Feb 8 at 8:38
ilkkachu
59.6k894168
59.6k894168
asked Feb 8 at 6:53
user335828user335828
1
1
1
Use"${var}"instead of just$var;${x}${y}instead of$x$y. See unix.stackexchange.com/questions/4899/…
– Kenneth B. Jensen
Feb 8 at 6:57
2
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
@MichaelHomer I've performed several more tests; turns out that I was printing just$y, which was identical to$x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.
– Kenneth B. Jensen
Feb 8 at 7:20
1
@Kusalananda, the addition works since they're declared integer variables. Evencounter+=7would do. Though I'm not sure if it would be more readable to just use$(( .. ))with regular variables ...
– ilkkachu
Feb 8 at 8:52
|
show 2 more comments
1
Use"${var}"instead of just$var;${x}${y}instead of$x$y. See unix.stackexchange.com/questions/4899/…
– Kenneth B. Jensen
Feb 8 at 6:57
2
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
@MichaelHomer I've performed several more tests; turns out that I was printing just$y, which was identical to$x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.
– Kenneth B. Jensen
Feb 8 at 7:20
1
@Kusalananda, the addition works since they're declared integer variables. Evencounter+=7would do. Though I'm not sure if it would be more readable to just use$(( .. ))with regular variables ...
– ilkkachu
Feb 8 at 8:52
1
1
Use
"${var}" instead of just $var; ${x}${y} instead of $x$y. See unix.stackexchange.com/questions/4899/…– Kenneth B. Jensen
Feb 8 at 6:57
Use
"${var}" instead of just $var; ${x}${y} instead of $x$y. See unix.stackexchange.com/questions/4899/…– Kenneth B. Jensen
Feb 8 at 6:57
2
2
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
@MichaelHomer I've performed several more tests; turns out that I was printing just
$y, which was identical to $x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.– Kenneth B. Jensen
Feb 8 at 7:20
@MichaelHomer I've performed several more tests; turns out that I was printing just
$y, which was identical to $x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.– Kenneth B. Jensen
Feb 8 at 7:20
1
1
@Kusalananda, the addition works since they're declared integer variables. Even
counter+=7 would do. Though I'm not sure if it would be more readable to just use $(( .. )) with regular variables ...– ilkkachu
Feb 8 at 8:52
@Kusalananda, the addition works since they're declared integer variables. Even
counter+=7 would do. Though I'm not sure if it would be more readable to just use $(( .. )) with regular variables ...– ilkkachu
Feb 8 at 8:52
|
show 2 more comments
3 Answers
3
active
oldest
votes
Use an array instead, that allows you to properly index the variables:
#!/bin/bash
declare -ai counters=(11 14) # indexes start from 0
v1=()
v2=()
for i in {1..2}; do
v1[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter1="$counter" 'NR==counter1' | awk -F" '{print $4}')
counters[0]+=7
v2[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter3="$counter2" 'NR==counter3' | awk -F" '{print $4}')
counters[1]+=7
echo "${v1[$i]}"
echo "${v2[$i]}"
done
You could probably put the vN assignments within another for loop to reduce the repetition.
Of course, if you're not using v1 and v2 for anything other than displaying the values once, you could just run aws ... | awk directly and skip the variables and the echo.
add a comment |
I don't know what your code is supposed to accomplish but what it does is this:
In the first iteration - when i equals 1 - it declares variables v11 and v12 and assigns something to them and then prints $v1$i to the screen.
v1 is non-existent and $i is the value of i, so you should get 'nothing' with an appended 1 as output in the first loop:
And then in the second iteration - when i equals 2 - two times 2 for the same reason.
What you do after the equal sign with awk etc. is irrelevant for the outcome.
The suggestion to use an array and use $i as an index into it will solve the problem.
add a comment |
You seem to want to output the fourth "-delimited field on lines 11, 17, 18, and 21 of the output of an aws command.
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }'
This calls aws once instead of four times, and calls awk once instead of eight times.
If you want this in an array:
readarray -t output < <(
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }' )
The array output would now contain the data with one line from awk in each element, starting at index 0.
If you want to split that array into two, so that you get element 0 and 2 in one and 1 and 3 in the other:
v1=( "${output[0]}" "${output[2]}" )
v2=( "${output[1]}" "${output[3]}" )
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%2f499415%2fprinting-a-variable-which-contains-sign%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use an array instead, that allows you to properly index the variables:
#!/bin/bash
declare -ai counters=(11 14) # indexes start from 0
v1=()
v2=()
for i in {1..2}; do
v1[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter1="$counter" 'NR==counter1' | awk -F" '{print $4}')
counters[0]+=7
v2[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter3="$counter2" 'NR==counter3' | awk -F" '{print $4}')
counters[1]+=7
echo "${v1[$i]}"
echo "${v2[$i]}"
done
You could probably put the vN assignments within another for loop to reduce the repetition.
Of course, if you're not using v1 and v2 for anything other than displaying the values once, you could just run aws ... | awk directly and skip the variables and the echo.
add a comment |
Use an array instead, that allows you to properly index the variables:
#!/bin/bash
declare -ai counters=(11 14) # indexes start from 0
v1=()
v2=()
for i in {1..2}; do
v1[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter1="$counter" 'NR==counter1' | awk -F" '{print $4}')
counters[0]+=7
v2[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter3="$counter2" 'NR==counter3' | awk -F" '{print $4}')
counters[1]+=7
echo "${v1[$i]}"
echo "${v2[$i]}"
done
You could probably put the vN assignments within another for loop to reduce the repetition.
Of course, if you're not using v1 and v2 for anything other than displaying the values once, you could just run aws ... | awk directly and skip the variables and the echo.
add a comment |
Use an array instead, that allows you to properly index the variables:
#!/bin/bash
declare -ai counters=(11 14) # indexes start from 0
v1=()
v2=()
for i in {1..2}; do
v1[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter1="$counter" 'NR==counter1' | awk -F" '{print $4}')
counters[0]+=7
v2[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter3="$counter2" 'NR==counter3' | awk -F" '{print $4}')
counters[1]+=7
echo "${v1[$i]}"
echo "${v2[$i]}"
done
You could probably put the vN assignments within another for loop to reduce the repetition.
Of course, if you're not using v1 and v2 for anything other than displaying the values once, you could just run aws ... | awk directly and skip the variables and the echo.
Use an array instead, that allows you to properly index the variables:
#!/bin/bash
declare -ai counters=(11 14) # indexes start from 0
v1=()
v2=()
for i in {1..2}; do
v1[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter1="$counter" 'NR==counter1' | awk -F" '{print $4}')
counters[0]+=7
v2[$i]=$(aws iam get-group --group-name VideoEditors | awk -v counter3="$counter2" 'NR==counter3' | awk -F" '{print $4}')
counters[1]+=7
echo "${v1[$i]}"
echo "${v2[$i]}"
done
You could probably put the vN assignments within another for loop to reduce the repetition.
Of course, if you're not using v1 and v2 for anything other than displaying the values once, you could just run aws ... | awk directly and skip the variables and the echo.
answered Feb 8 at 8:48
ilkkachuilkkachu
59.6k894168
59.6k894168
add a comment |
add a comment |
I don't know what your code is supposed to accomplish but what it does is this:
In the first iteration - when i equals 1 - it declares variables v11 and v12 and assigns something to them and then prints $v1$i to the screen.
v1 is non-existent and $i is the value of i, so you should get 'nothing' with an appended 1 as output in the first loop:
And then in the second iteration - when i equals 2 - two times 2 for the same reason.
What you do after the equal sign with awk etc. is irrelevant for the outcome.
The suggestion to use an array and use $i as an index into it will solve the problem.
add a comment |
I don't know what your code is supposed to accomplish but what it does is this:
In the first iteration - when i equals 1 - it declares variables v11 and v12 and assigns something to them and then prints $v1$i to the screen.
v1 is non-existent and $i is the value of i, so you should get 'nothing' with an appended 1 as output in the first loop:
And then in the second iteration - when i equals 2 - two times 2 for the same reason.
What you do after the equal sign with awk etc. is irrelevant for the outcome.
The suggestion to use an array and use $i as an index into it will solve the problem.
add a comment |
I don't know what your code is supposed to accomplish but what it does is this:
In the first iteration - when i equals 1 - it declares variables v11 and v12 and assigns something to them and then prints $v1$i to the screen.
v1 is non-existent and $i is the value of i, so you should get 'nothing' with an appended 1 as output in the first loop:
And then in the second iteration - when i equals 2 - two times 2 for the same reason.
What you do after the equal sign with awk etc. is irrelevant for the outcome.
The suggestion to use an array and use $i as an index into it will solve the problem.
I don't know what your code is supposed to accomplish but what it does is this:
In the first iteration - when i equals 1 - it declares variables v11 and v12 and assigns something to them and then prints $v1$i to the screen.
v1 is non-existent and $i is the value of i, so you should get 'nothing' with an appended 1 as output in the first loop:
And then in the second iteration - when i equals 2 - two times 2 for the same reason.
What you do after the equal sign with awk etc. is irrelevant for the outcome.
The suggestion to use an array and use $i as an index into it will solve the problem.
answered Feb 8 at 9:14
ArjenArjen
686
686
add a comment |
add a comment |
You seem to want to output the fourth "-delimited field on lines 11, 17, 18, and 21 of the output of an aws command.
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }'
This calls aws once instead of four times, and calls awk once instead of eight times.
If you want this in an array:
readarray -t output < <(
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }' )
The array output would now contain the data with one line from awk in each element, starting at index 0.
If you want to split that array into two, so that you get element 0 and 2 in one and 1 and 3 in the other:
v1=( "${output[0]}" "${output[2]}" )
v2=( "${output[1]}" "${output[3]}" )
add a comment |
You seem to want to output the fourth "-delimited field on lines 11, 17, 18, and 21 of the output of an aws command.
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }'
This calls aws once instead of four times, and calls awk once instead of eight times.
If you want this in an array:
readarray -t output < <(
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }' )
The array output would now contain the data with one line from awk in each element, starting at index 0.
If you want to split that array into two, so that you get element 0 and 2 in one and 1 and 3 in the other:
v1=( "${output[0]}" "${output[2]}" )
v2=( "${output[1]}" "${output[3]}" )
add a comment |
You seem to want to output the fourth "-delimited field on lines 11, 17, 18, and 21 of the output of an aws command.
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }'
This calls aws once instead of four times, and calls awk once instead of eight times.
If you want this in an array:
readarray -t output < <(
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }' )
The array output would now contain the data with one line from awk in each element, starting at index 0.
If you want to split that array into two, so that you get element 0 and 2 in one and 1 and 3 in the other:
v1=( "${output[0]}" "${output[2]}" )
v2=( "${output[1]}" "${output[3]}" )
You seem to want to output the fourth "-delimited field on lines 11, 17, 18, and 21 of the output of an aws command.
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }'
This calls aws once instead of four times, and calls awk once instead of eight times.
If you want this in an array:
readarray -t output < <(
aws iam get-group --group-name VideoEditors |
awk -F '"' 'NR == 11 || NR == 17 || NR == 18 || NR == 21 { print $4 }' )
The array output would now contain the data with one line from awk in each element, starting at index 0.
If you want to split that array into two, so that you get element 0 and 2 in one and 1 and 3 in the other:
v1=( "${output[0]}" "${output[2]}" )
v2=( "${output[1]}" "${output[3]}" )
answered Feb 8 at 9:51
KusalanandaKusalananda
131k17250409
131k17250409
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%2f499415%2fprinting-a-variable-which-contains-sign%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
1
Use
"${var}"instead of just$var;${x}${y}instead of$x$y. See unix.stackexchange.com/questions/4899/…– Kenneth B. Jensen
Feb 8 at 6:57
2
I don't see any way that the braces would make a lick of difference there.
– Michael Homer
Feb 8 at 7:07
Kenneth, I tried your solution but result is the same. The output is: taskscript.sh: line 16: ${v1$i}: bad substitution
– user335832
Feb 8 at 7:18
@MichaelHomer I've performed several more tests; turns out that I was printing just
$y, which was identical to$x$y. OP, my advice would be to use an array for v1 and v2 instead of v11/v12/v21/v22/etc.– Kenneth B. Jensen
Feb 8 at 7:20
1
@Kusalananda, the addition works since they're declared integer variables. Even
counter+=7would do. Though I'm not sure if it would be more readable to just use$(( .. ))with regular variables ...– ilkkachu
Feb 8 at 8:52