How to Increment Variables dynamically
Clash Royale CLAN TAG#URR8PPP
up vote
-3
down vote
favorite
How to increment variables dynamically.
I have written below script but it is giving an error.
#!/bin/bash
X='101 Hari BAN'
for i in "$X[@]"
do
"V"$j=`echo $i|cut -d' ' -f$j`
echo "V"$j
j=`expr $j + 1`
done
Output should be,
V1=101
V2=Hari
V3=BAN
bash variable
add a comment |
up vote
-3
down vote
favorite
How to increment variables dynamically.
I have written below script but it is giving an error.
#!/bin/bash
X='101 Hari BAN'
for i in "$X[@]"
do
"V"$j=`echo $i|cut -d' ' -f$j`
echo "V"$j
j=`expr $j + 1`
done
Output should be,
V1=101
V2=Hari
V3=BAN
bash variable
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
How to increment variables dynamically.
I have written below script but it is giving an error.
#!/bin/bash
X='101 Hari BAN'
for i in "$X[@]"
do
"V"$j=`echo $i|cut -d' ' -f$j`
echo "V"$j
j=`expr $j + 1`
done
Output should be,
V1=101
V2=Hari
V3=BAN
bash variable
How to increment variables dynamically.
I have written below script but it is giving an error.
#!/bin/bash
X='101 Hari BAN'
for i in "$X[@]"
do
"V"$j=`echo $i|cut -d' ' -f$j`
echo "V"$j
j=`expr $j + 1`
done
Output should be,
V1=101
V2=Hari
V3=BAN
bash variable
bash variable
edited Nov 28 at 17:03
Kusalananda
118k16223364
118k16223364
asked Nov 28 at 15:11
Harish a
233
233
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
First of all, if you are using bash
and you want to iterate over separate things, then use an array:
X=( 101 Hari BAN "many words" "a * in the sky" )
This allows you to handle string that contain whitespaces and filename globbing patterns correctly by appropriately quoting those strings.
Then loop over the array:
X=( 101 Hari BAN "many words" "a * in the sky" )
for item in "$X[@]"; do
done
Then, it's a matter of outputting the result. For this we use a counter and printf
:
X=( 101 Hari BAN "many words" "a * in the sky" )
n=0
for item in "$X[@]"; do
printf 'V%d=%sn' "$(( ++n ))" "$item"
done
The printf
format string V%d=%sn
means "The character V
followed by an integer, then a =
and some string. End with newline". The integer and string is taken from the remaining arguments to printf
. The variable n
is incremented before being used by the printf
.
Add the correct #!
-line pointing to your bash
interpreter and you're done.
The output would be
V1=101
V2=Hari
V3=BAN
V4=many words
V5=a * in the sky
According to comments below, you were expecting V1
, V2
to be variables, but they obviously are just text in the output.
Again, what I think you need here is not separate variables but an array.
In fact, the array that we already have, X
already contains the necessary data, so we could just rename it.
#!/bin/bash
V=( 101 Hari BAN )
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
done
That is, you simply use $V[0]
, $V[1]
, etc. to access the elements of the array V
.
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
First of all, if you are using bash
and you want to iterate over separate things, then use an array:
X=( 101 Hari BAN "many words" "a * in the sky" )
This allows you to handle string that contain whitespaces and filename globbing patterns correctly by appropriately quoting those strings.
Then loop over the array:
X=( 101 Hari BAN "many words" "a * in the sky" )
for item in "$X[@]"; do
done
Then, it's a matter of outputting the result. For this we use a counter and printf
:
X=( 101 Hari BAN "many words" "a * in the sky" )
n=0
for item in "$X[@]"; do
printf 'V%d=%sn' "$(( ++n ))" "$item"
done
The printf
format string V%d=%sn
means "The character V
followed by an integer, then a =
and some string. End with newline". The integer and string is taken from the remaining arguments to printf
. The variable n
is incremented before being used by the printf
.
Add the correct #!
-line pointing to your bash
interpreter and you're done.
The output would be
V1=101
V2=Hari
V3=BAN
V4=many words
V5=a * in the sky
According to comments below, you were expecting V1
, V2
to be variables, but they obviously are just text in the output.
Again, what I think you need here is not separate variables but an array.
In fact, the array that we already have, X
already contains the necessary data, so we could just rename it.
#!/bin/bash
V=( 101 Hari BAN )
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
done
That is, you simply use $V[0]
, $V[1]
, etc. to access the elements of the array V
.
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
add a comment |
up vote
2
down vote
accepted
First of all, if you are using bash
and you want to iterate over separate things, then use an array:
X=( 101 Hari BAN "many words" "a * in the sky" )
This allows you to handle string that contain whitespaces and filename globbing patterns correctly by appropriately quoting those strings.
Then loop over the array:
X=( 101 Hari BAN "many words" "a * in the sky" )
for item in "$X[@]"; do
done
Then, it's a matter of outputting the result. For this we use a counter and printf
:
X=( 101 Hari BAN "many words" "a * in the sky" )
n=0
for item in "$X[@]"; do
printf 'V%d=%sn' "$(( ++n ))" "$item"
done
The printf
format string V%d=%sn
means "The character V
followed by an integer, then a =
and some string. End with newline". The integer and string is taken from the remaining arguments to printf
. The variable n
is incremented before being used by the printf
.
Add the correct #!
-line pointing to your bash
interpreter and you're done.
The output would be
V1=101
V2=Hari
V3=BAN
V4=many words
V5=a * in the sky
According to comments below, you were expecting V1
, V2
to be variables, but they obviously are just text in the output.
Again, what I think you need here is not separate variables but an array.
In fact, the array that we already have, X
already contains the necessary data, so we could just rename it.
#!/bin/bash
V=( 101 Hari BAN )
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
done
That is, you simply use $V[0]
, $V[1]
, etc. to access the elements of the array V
.
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
First of all, if you are using bash
and you want to iterate over separate things, then use an array:
X=( 101 Hari BAN "many words" "a * in the sky" )
This allows you to handle string that contain whitespaces and filename globbing patterns correctly by appropriately quoting those strings.
Then loop over the array:
X=( 101 Hari BAN "many words" "a * in the sky" )
for item in "$X[@]"; do
done
Then, it's a matter of outputting the result. For this we use a counter and printf
:
X=( 101 Hari BAN "many words" "a * in the sky" )
n=0
for item in "$X[@]"; do
printf 'V%d=%sn' "$(( ++n ))" "$item"
done
The printf
format string V%d=%sn
means "The character V
followed by an integer, then a =
and some string. End with newline". The integer and string is taken from the remaining arguments to printf
. The variable n
is incremented before being used by the printf
.
Add the correct #!
-line pointing to your bash
interpreter and you're done.
The output would be
V1=101
V2=Hari
V3=BAN
V4=many words
V5=a * in the sky
According to comments below, you were expecting V1
, V2
to be variables, but they obviously are just text in the output.
Again, what I think you need here is not separate variables but an array.
In fact, the array that we already have, X
already contains the necessary data, so we could just rename it.
#!/bin/bash
V=( 101 Hari BAN )
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
done
That is, you simply use $V[0]
, $V[1]
, etc. to access the elements of the array V
.
First of all, if you are using bash
and you want to iterate over separate things, then use an array:
X=( 101 Hari BAN "many words" "a * in the sky" )
This allows you to handle string that contain whitespaces and filename globbing patterns correctly by appropriately quoting those strings.
Then loop over the array:
X=( 101 Hari BAN "many words" "a * in the sky" )
for item in "$X[@]"; do
done
Then, it's a matter of outputting the result. For this we use a counter and printf
:
X=( 101 Hari BAN "many words" "a * in the sky" )
n=0
for item in "$X[@]"; do
printf 'V%d=%sn' "$(( ++n ))" "$item"
done
The printf
format string V%d=%sn
means "The character V
followed by an integer, then a =
and some string. End with newline". The integer and string is taken from the remaining arguments to printf
. The variable n
is incremented before being used by the printf
.
Add the correct #!
-line pointing to your bash
interpreter and you're done.
The output would be
V1=101
V2=Hari
V3=BAN
V4=many words
V5=a * in the sky
According to comments below, you were expecting V1
, V2
to be variables, but they obviously are just text in the output.
Again, what I think you need here is not separate variables but an array.
In fact, the array that we already have, X
already contains the necessary data, so we could just rename it.
#!/bin/bash
V=( 101 Hari BAN )
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
done
That is, you simply use $V[0]
, $V[1]
, etc. to access the elements of the array V
.
edited Nov 29 at 16:28
answered Nov 28 at 17:17
Kusalananda
118k16223364
118k16223364
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
add a comment |
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
Thank you. It is working. But I'm unable to use V1,V2,V3,V4,V5 variables. So can you please help me.
– Harish a
Nov 29 at 16:16
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
@Harisha You said nothing in the question about these being variables. You said you wanted a particular output... I'll make an edit, hold on.
– Kusalananda
Nov 29 at 16:21
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
Thanks again. It is working.
– Harish a
Nov 29 at 16:46
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
@Harisha Hi. I don't give out personal information like that to people on this site (and you shouldn't either). If you have further questions, you should ask a new question on the site. If needed, you can provide a link to this question in the new question as a reference and then say what the new issue is.
– Kusalananda
Nov 30 at 12:59
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
It's difficult to read unformatted code. You should ask a new question, as I suggested, not add further questions in comments.
– Kusalananda
Nov 30 at 13:37
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f484689%2fhow-to-increment-variables-dynamically%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