How to Use an array of strings in bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I writte the following script



load function should set the array disk[a]=1 and disk[b]=2 and so on



Then the function out should print the array $disk[a] , and $disk[b] , and so on



But what we get from function out is always the number 4



Instead, I want to get the following:



1
2
3
4


What is wrong here ?



How to fix it so function out will print:



1
2
3
4


the script:



#!/bin/bash

function load

counter=1
for input in a b c d
do
disk[$input]=$counter
let counter=$counter+1
echo $disk[$input]
done



function out

counter=1
for input in a b c d
do
echo $disk[$input]
let counter=$counter+1
done


echo "run function load"
load
echo "run function out"
out


the output:



./test
run function load
1
2
3
4
run function out
4
4
4
4









share|improve this question



























    up vote
    1
    down vote

    favorite












    I writte the following script



    load function should set the array disk[a]=1 and disk[b]=2 and so on



    Then the function out should print the array $disk[a] , and $disk[b] , and so on



    But what we get from function out is always the number 4



    Instead, I want to get the following:



    1
    2
    3
    4


    What is wrong here ?



    How to fix it so function out will print:



    1
    2
    3
    4


    the script:



    #!/bin/bash

    function load

    counter=1
    for input in a b c d
    do
    disk[$input]=$counter
    let counter=$counter+1
    echo $disk[$input]
    done



    function out

    counter=1
    for input in a b c d
    do
    echo $disk[$input]
    let counter=$counter+1
    done


    echo "run function load"
    load
    echo "run function out"
    out


    the output:



    ./test
    run function load
    1
    2
    3
    4
    run function out
    4
    4
    4
    4









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I writte the following script



      load function should set the array disk[a]=1 and disk[b]=2 and so on



      Then the function out should print the array $disk[a] , and $disk[b] , and so on



      But what we get from function out is always the number 4



      Instead, I want to get the following:



      1
      2
      3
      4


      What is wrong here ?



      How to fix it so function out will print:



      1
      2
      3
      4


      the script:



      #!/bin/bash

      function load

      counter=1
      for input in a b c d
      do
      disk[$input]=$counter
      let counter=$counter+1
      echo $disk[$input]
      done



      function out

      counter=1
      for input in a b c d
      do
      echo $disk[$input]
      let counter=$counter+1
      done


      echo "run function load"
      load
      echo "run function out"
      out


      the output:



      ./test
      run function load
      1
      2
      3
      4
      run function out
      4
      4
      4
      4









      share|improve this question















      I writte the following script



      load function should set the array disk[a]=1 and disk[b]=2 and so on



      Then the function out should print the array $disk[a] , and $disk[b] , and so on



      But what we get from function out is always the number 4



      Instead, I want to get the following:



      1
      2
      3
      4


      What is wrong here ?



      How to fix it so function out will print:



      1
      2
      3
      4


      the script:



      #!/bin/bash

      function load

      counter=1
      for input in a b c d
      do
      disk[$input]=$counter
      let counter=$counter+1
      echo $disk[$input]
      done



      function out

      counter=1
      for input in a b c d
      do
      echo $disk[$input]
      let counter=$counter+1
      done


      echo "run function load"
      load
      echo "run function out"
      out


      the output:



      ./test
      run function load
      1
      2
      3
      4
      run function out
      4
      4
      4
      4






      linux bash shell-script array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago









      Jeff Schaller

      35.1k952115




      35.1k952115










      asked 3 hours ago









      yael

      2,1401650




      2,1401650




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Bash arrays are indexed arrays by default:




          An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value




          ... but you are using letters as the index, so you probably want an associative array, which means you need an:



          declare -A disk


          before calling the functions.






          share|improve this answer




















          • what actually declare -A do?
            – yael
            2 hours ago










          • @yael, it declares an associative array
            – Jeff Schaller
            2 hours ago

















          up vote
          3
          down vote













          Look at what happens to the array when you initialize it:



          $ i=0; for k in a b c d; do A[$k]=$((i++)); done; declare -p A
          declare -a A=([0]="3")


          Only one element is present, and it has the index zero.



          By default, arrays are indexed by numbers, and the numerical values of the indices you used happened to be all zero. Actually, in an arithmetic context, like the subscript of a regular array, a string is taken as the name of a variable, and the value of that variable is used. So, if we set a, b... to numbers, then we get something different:



          $ a=123; b=456; c=789; d=999; i=0;
          $ for k in a b c d; do A[$k]=$((i++)); done;
          $ declare -p A
          declare -a A=([123]="0" [456]="1" [789]="2" [999]="3")


          To actually use the strings themselves as indices, declare the array as an associative array first with declare -A arrayname or typeset -A arrayname:



          $ unset A; declare -A A; i=0;
          $ for k in a b c d; do A[$k]=$((i++)); done; declare -p A
          declare -A A=([a]="0" [b]="1" [c]="2" [d]="3" )





          share|improve this answer




















            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%2f479177%2fhow-to-use-an-array-of-strings-in-bash%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            Bash arrays are indexed arrays by default:




            An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value




            ... but you are using letters as the index, so you probably want an associative array, which means you need an:



            declare -A disk


            before calling the functions.






            share|improve this answer




















            • what actually declare -A do?
              – yael
              2 hours ago










            • @yael, it declares an associative array
              – Jeff Schaller
              2 hours ago














            up vote
            4
            down vote



            accepted










            Bash arrays are indexed arrays by default:




            An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value




            ... but you are using letters as the index, so you probably want an associative array, which means you need an:



            declare -A disk


            before calling the functions.






            share|improve this answer




















            • what actually declare -A do?
              – yael
              2 hours ago










            • @yael, it declares an associative array
              – Jeff Schaller
              2 hours ago












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Bash arrays are indexed arrays by default:




            An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value




            ... but you are using letters as the index, so you probably want an associative array, which means you need an:



            declare -A disk


            before calling the functions.






            share|improve this answer












            Bash arrays are indexed arrays by default:




            An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value




            ... but you are using letters as the index, so you probably want an associative array, which means you need an:



            declare -A disk


            before calling the functions.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            Jeff Schaller

            35.1k952115




            35.1k952115











            • what actually declare -A do?
              – yael
              2 hours ago










            • @yael, it declares an associative array
              – Jeff Schaller
              2 hours ago
















            • what actually declare -A do?
              – yael
              2 hours ago










            • @yael, it declares an associative array
              – Jeff Schaller
              2 hours ago















            what actually declare -A do?
            – yael
            2 hours ago




            what actually declare -A do?
            – yael
            2 hours ago












            @yael, it declares an associative array
            – Jeff Schaller
            2 hours ago




            @yael, it declares an associative array
            – Jeff Schaller
            2 hours ago












            up vote
            3
            down vote













            Look at what happens to the array when you initialize it:



            $ i=0; for k in a b c d; do A[$k]=$((i++)); done; declare -p A
            declare -a A=([0]="3")


            Only one element is present, and it has the index zero.



            By default, arrays are indexed by numbers, and the numerical values of the indices you used happened to be all zero. Actually, in an arithmetic context, like the subscript of a regular array, a string is taken as the name of a variable, and the value of that variable is used. So, if we set a, b... to numbers, then we get something different:



            $ a=123; b=456; c=789; d=999; i=0;
            $ for k in a b c d; do A[$k]=$((i++)); done;
            $ declare -p A
            declare -a A=([123]="0" [456]="1" [789]="2" [999]="3")


            To actually use the strings themselves as indices, declare the array as an associative array first with declare -A arrayname or typeset -A arrayname:



            $ unset A; declare -A A; i=0;
            $ for k in a b c d; do A[$k]=$((i++)); done; declare -p A
            declare -A A=([a]="0" [b]="1" [c]="2" [d]="3" )





            share|improve this answer
























              up vote
              3
              down vote













              Look at what happens to the array when you initialize it:



              $ i=0; for k in a b c d; do A[$k]=$((i++)); done; declare -p A
              declare -a A=([0]="3")


              Only one element is present, and it has the index zero.



              By default, arrays are indexed by numbers, and the numerical values of the indices you used happened to be all zero. Actually, in an arithmetic context, like the subscript of a regular array, a string is taken as the name of a variable, and the value of that variable is used. So, if we set a, b... to numbers, then we get something different:



              $ a=123; b=456; c=789; d=999; i=0;
              $ for k in a b c d; do A[$k]=$((i++)); done;
              $ declare -p A
              declare -a A=([123]="0" [456]="1" [789]="2" [999]="3")


              To actually use the strings themselves as indices, declare the array as an associative array first with declare -A arrayname or typeset -A arrayname:



              $ unset A; declare -A A; i=0;
              $ for k in a b c d; do A[$k]=$((i++)); done; declare -p A
              declare -A A=([a]="0" [b]="1" [c]="2" [d]="3" )





              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                Look at what happens to the array when you initialize it:



                $ i=0; for k in a b c d; do A[$k]=$((i++)); done; declare -p A
                declare -a A=([0]="3")


                Only one element is present, and it has the index zero.



                By default, arrays are indexed by numbers, and the numerical values of the indices you used happened to be all zero. Actually, in an arithmetic context, like the subscript of a regular array, a string is taken as the name of a variable, and the value of that variable is used. So, if we set a, b... to numbers, then we get something different:



                $ a=123; b=456; c=789; d=999; i=0;
                $ for k in a b c d; do A[$k]=$((i++)); done;
                $ declare -p A
                declare -a A=([123]="0" [456]="1" [789]="2" [999]="3")


                To actually use the strings themselves as indices, declare the array as an associative array first with declare -A arrayname or typeset -A arrayname:



                $ unset A; declare -A A; i=0;
                $ for k in a b c d; do A[$k]=$((i++)); done; declare -p A
                declare -A A=([a]="0" [b]="1" [c]="2" [d]="3" )





                share|improve this answer












                Look at what happens to the array when you initialize it:



                $ i=0; for k in a b c d; do A[$k]=$((i++)); done; declare -p A
                declare -a A=([0]="3")


                Only one element is present, and it has the index zero.



                By default, arrays are indexed by numbers, and the numerical values of the indices you used happened to be all zero. Actually, in an arithmetic context, like the subscript of a regular array, a string is taken as the name of a variable, and the value of that variable is used. So, if we set a, b... to numbers, then we get something different:



                $ a=123; b=456; c=789; d=999; i=0;
                $ for k in a b c d; do A[$k]=$((i++)); done;
                $ declare -p A
                declare -a A=([123]="0" [456]="1" [789]="2" [999]="3")


                To actually use the strings themselves as indices, declare the array as an associative array first with declare -A arrayname or typeset -A arrayname:



                $ unset A; declare -A A; i=0;
                $ for k in a b c d; do A[$k]=$((i++)); done; declare -p A
                declare -A A=([a]="0" [b]="1" [c]="2" [d]="3" )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 3 hours ago









                ilkkachu

                53.3k780146




                53.3k780146



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479177%2fhow-to-use-an-array-of-strings-in-bash%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery