How to Increment Variables dynamically

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question



























    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









    share|improve this question

























      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 at 17:03









      Kusalananda

      118k16223364




      118k16223364










      asked Nov 28 at 15:11









      Harish a

      233




      233




















          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.






          share|improve this answer






















          • 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










          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',
          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
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer






















          • 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














          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.






          share|improve this answer






















          • 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












          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?