Print out argument value in for loopWhy is printf better than echo?When is double-quoting necessary?Parse...

Find max number you can create from an array of numbers

Why do Klingons use cloaking devices?

Machine Learning Golf: Multiplication

Should I cheat if the majority does it?

How did Captain Marvel do this without dying?

How serious is plagiarism in a master’s thesis?

Sleepy tired vs physically tired

Taking my Ph.D. advisor out for dinner after graduation

How can I create a dashed line that slowly changes into a solid line in Illustrator?

2000s (or earlier) cyberpunk novel: dystopia, mega storm, omnipresent noise

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

An elegant aspect for enumerate the equations of a book

How to reclaim personal item I've lent to the office without burning bridges?

Bypass with wrong cvv of debit card and getting OTP

What's the difference between a type and a kind?

Taking advantage when the HR forgets to communicate the rules

n-level Ouroboros Quine

Why do most airliners have underwing engines, while business jets have rear-mounted engines?

PhD: When to quit and move on?

How did the IEC decide to create kibibytes?

Initializing variables variable in an "if" statement

Does 5e have an equivalent of the Psychic Paper from Doctor Who?

Question about targeting a Hexproof creature

How do I check that users don't write down their passwords?



Print out argument value in for loop


Why is printf better than echo?When is double-quoting necessary?Parse header in a file and based on the header replace a value in the fileHow do I get the value of a named option of an already running process in Linux?echo $variable > file in a loop outputs only the last value of variableFor loop separate $i'sget first CLI argument after the options in shell sciptInfinite while loop issue using readUsing shell script argument?Use argument 2 until @ for loopPass function argument to defined variableHow to print out each command argument with echo?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







-1















This is the script



user@linux:~$ cat script.sh 
for i in `seq $#`
do
echo $i
done
user@linux:~$


Output



user@linux:~$ ./script.sh a b c
1
2
3
user@linux:~$


Desired Output



I would like to get argument value like this .... and not just the number



user@linux:~$ ./script.sh a b c
1 - a
2 - b
3 - c
user@linux:~$









share|improve this question























  • You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

    – Stéphane Chazelas
    2 mins ago


















-1















This is the script



user@linux:~$ cat script.sh 
for i in `seq $#`
do
echo $i
done
user@linux:~$


Output



user@linux:~$ ./script.sh a b c
1
2
3
user@linux:~$


Desired Output



I would like to get argument value like this .... and not just the number



user@linux:~$ ./script.sh a b c
1 - a
2 - b
3 - c
user@linux:~$









share|improve this question























  • You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

    – Stéphane Chazelas
    2 mins ago














-1












-1








-1








This is the script



user@linux:~$ cat script.sh 
for i in `seq $#`
do
echo $i
done
user@linux:~$


Output



user@linux:~$ ./script.sh a b c
1
2
3
user@linux:~$


Desired Output



I would like to get argument value like this .... and not just the number



user@linux:~$ ./script.sh a b c
1 - a
2 - b
3 - c
user@linux:~$









share|improve this question














This is the script



user@linux:~$ cat script.sh 
for i in `seq $#`
do
echo $i
done
user@linux:~$


Output



user@linux:~$ ./script.sh a b c
1
2
3
user@linux:~$


Desired Output



I would like to get argument value like this .... and not just the number



user@linux:~$ ./script.sh a b c
1 - a
2 - b
3 - c
user@linux:~$






bash arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 28 mins ago









SabrinaSabrina

3841 silver badge11 bronze badges




3841 silver badge11 bronze badges













  • You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

    – Stéphane Chazelas
    2 mins ago



















  • You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

    – Stéphane Chazelas
    2 mins ago

















You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

– Stéphane Chazelas
2 mins ago





You included the bash tag, but gave your script a .sh extension, and the syntax of the file looks more like zsh (because of the unquoted variable expansions). Which is it?

– Stéphane Chazelas
2 mins ago










2 Answers
2






active

oldest

votes


















1














#! /bin/sh -
i=1
for arg do
printf '%2d: %sn' "$i" "$arg"
i=$((i + 1))
done


That is, instead of looping over the indexes, loop over the arguments and increment an index separately.



A few notes on your approach:




  • leaving a parameter expansion unquoted in list context has a very special meaning, you almost never want to do that.


  • echo can't be used to output arbitrary data.

  • The `...` form of command substitution should be avoided these days. Use $(...) instead.

  • Because unquoted command substitution also invokes split+glob, it has to be used with care. Here, you're using the split part which means you have a dependency on the current value of $IFS. The output of seq won't include wildcards, so you won't have a problem with the glob part.


  • seq is not a standard command and not found on all systems. To loop on numbers, it's not the best approach anyway, as that means storing the whole output in memory and split it (resulting in extra copies in memory), and that means running a separate utility in a separate process even though the shell has builtin support for that.






share|improve this answer

































    0














    #!/bin/bash
    for ((i=1; i<=$#; i++))
    do
    echo "$i - ${!i}"
    done




    share


























      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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f528311%2fprint-out-argument-value-in-for-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









      1














      #! /bin/sh -
      i=1
      for arg do
      printf '%2d: %sn' "$i" "$arg"
      i=$((i + 1))
      done


      That is, instead of looping over the indexes, loop over the arguments and increment an index separately.



      A few notes on your approach:




      • leaving a parameter expansion unquoted in list context has a very special meaning, you almost never want to do that.


      • echo can't be used to output arbitrary data.

      • The `...` form of command substitution should be avoided these days. Use $(...) instead.

      • Because unquoted command substitution also invokes split+glob, it has to be used with care. Here, you're using the split part which means you have a dependency on the current value of $IFS. The output of seq won't include wildcards, so you won't have a problem with the glob part.


      • seq is not a standard command and not found on all systems. To loop on numbers, it's not the best approach anyway, as that means storing the whole output in memory and split it (resulting in extra copies in memory), and that means running a separate utility in a separate process even though the shell has builtin support for that.






      share|improve this answer






























        1














        #! /bin/sh -
        i=1
        for arg do
        printf '%2d: %sn' "$i" "$arg"
        i=$((i + 1))
        done


        That is, instead of looping over the indexes, loop over the arguments and increment an index separately.



        A few notes on your approach:




        • leaving a parameter expansion unquoted in list context has a very special meaning, you almost never want to do that.


        • echo can't be used to output arbitrary data.

        • The `...` form of command substitution should be avoided these days. Use $(...) instead.

        • Because unquoted command substitution also invokes split+glob, it has to be used with care. Here, you're using the split part which means you have a dependency on the current value of $IFS. The output of seq won't include wildcards, so you won't have a problem with the glob part.


        • seq is not a standard command and not found on all systems. To loop on numbers, it's not the best approach anyway, as that means storing the whole output in memory and split it (resulting in extra copies in memory), and that means running a separate utility in a separate process even though the shell has builtin support for that.






        share|improve this answer




























          1












          1








          1







          #! /bin/sh -
          i=1
          for arg do
          printf '%2d: %sn' "$i" "$arg"
          i=$((i + 1))
          done


          That is, instead of looping over the indexes, loop over the arguments and increment an index separately.



          A few notes on your approach:




          • leaving a parameter expansion unquoted in list context has a very special meaning, you almost never want to do that.


          • echo can't be used to output arbitrary data.

          • The `...` form of command substitution should be avoided these days. Use $(...) instead.

          • Because unquoted command substitution also invokes split+glob, it has to be used with care. Here, you're using the split part which means you have a dependency on the current value of $IFS. The output of seq won't include wildcards, so you won't have a problem with the glob part.


          • seq is not a standard command and not found on all systems. To loop on numbers, it's not the best approach anyway, as that means storing the whole output in memory and split it (resulting in extra copies in memory), and that means running a separate utility in a separate process even though the shell has builtin support for that.






          share|improve this answer















          #! /bin/sh -
          i=1
          for arg do
          printf '%2d: %sn' "$i" "$arg"
          i=$((i + 1))
          done


          That is, instead of looping over the indexes, loop over the arguments and increment an index separately.



          A few notes on your approach:




          • leaving a parameter expansion unquoted in list context has a very special meaning, you almost never want to do that.


          • echo can't be used to output arbitrary data.

          • The `...` form of command substitution should be avoided these days. Use $(...) instead.

          • Because unquoted command substitution also invokes split+glob, it has to be used with care. Here, you're using the split part which means you have a dependency on the current value of $IFS. The output of seq won't include wildcards, so you won't have a problem with the glob part.


          • seq is not a standard command and not found on all systems. To loop on numbers, it's not the best approach anyway, as that means storing the whole output in memory and split it (resulting in extra copies in memory), and that means running a separate utility in a separate process even though the shell has builtin support for that.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 mins ago

























          answered 15 mins ago









          Stéphane ChazelasStéphane Chazelas

          324k57 gold badges627 silver badges996 bronze badges




          324k57 gold badges627 silver badges996 bronze badges

























              0














              #!/bin/bash
              for ((i=1; i<=$#; i++))
              do
              echo "$i - ${!i}"
              done




              share




























                0














                #!/bin/bash
                for ((i=1; i<=$#; i++))
                do
                echo "$i - ${!i}"
                done




                share


























                  0












                  0








                  0







                  #!/bin/bash
                  for ((i=1; i<=$#; i++))
                  do
                  echo "$i - ${!i}"
                  done




                  share













                  #!/bin/bash
                  for ((i=1; i<=$#; i++))
                  do
                  echo "$i - ${!i}"
                  done





                  share











                  share


                  share










                  answered 5 mins ago









                  FreddyFreddy

                  5,5161 gold badge6 silver badges23 bronze badges




                  5,5161 gold badge6 silver badges23 bronze badges






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f528311%2fprint-out-argument-value-in-for-loop%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

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...