Number of elements/words in a shell array variable












2














I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable.



But this only counts one word inside my variable so I have no idea how to fix it.



I have the following variables:



vmfarm1=(host1.com host2.com host3.com host4.com )
maximus=(host11.com host 12.com host 13.com)
firefly=(host5.com)


I need to find a way to count all the host names into a number inside the variables.



After this, the number inside that was counted, have to be used as a variable in this line.



I have tried:



echo "$input" | wc -w
printf ' n|/4.vmfarm1 ' >> textfile.txt


I have to write the 4 above by myself to the number and I need it to be done automatically, this is why I need a variable.










share|improve this question





























    2














    I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable.



    But this only counts one word inside my variable so I have no idea how to fix it.



    I have the following variables:



    vmfarm1=(host1.com host2.com host3.com host4.com )
    maximus=(host11.com host 12.com host 13.com)
    firefly=(host5.com)


    I need to find a way to count all the host names into a number inside the variables.



    After this, the number inside that was counted, have to be used as a variable in this line.



    I have tried:



    echo "$input" | wc -w
    printf ' n|/4.vmfarm1 ' >> textfile.txt


    I have to write the 4 above by myself to the number and I need it to be done automatically, this is why I need a variable.










    share|improve this question



























      2












      2








      2







      I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable.



      But this only counts one word inside my variable so I have no idea how to fix it.



      I have the following variables:



      vmfarm1=(host1.com host2.com host3.com host4.com )
      maximus=(host11.com host 12.com host 13.com)
      firefly=(host5.com)


      I need to find a way to count all the host names into a number inside the variables.



      After this, the number inside that was counted, have to be used as a variable in this line.



      I have tried:



      echo "$input" | wc -w
      printf ' n|/4.vmfarm1 ' >> textfile.txt


      I have to write the 4 above by myself to the number and I need it to be done automatically, this is why I need a variable.










      share|improve this question















      I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable.



      But this only counts one word inside my variable so I have no idea how to fix it.



      I have the following variables:



      vmfarm1=(host1.com host2.com host3.com host4.com )
      maximus=(host11.com host 12.com host 13.com)
      firefly=(host5.com)


      I need to find a way to count all the host names into a number inside the variables.



      After this, the number inside that was counted, have to be used as a variable in this line.



      I have tried:



      echo "$input" | wc -w
      printf ' n|/4.vmfarm1 ' >> textfile.txt


      I have to write the 4 above by myself to the number and I need it to be done automatically, this is why I need a variable.







      bash array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 at 12:21









      αғsнιη

      16.5k102865




      16.5k102865










      asked May 15 at 10:37









      TheSebM8

      94110




      94110






















          5 Answers
          5






          active

          oldest

          votes


















          7














          Given an array arr, its length (number of elements) is given by ${#arr[@]}.



          Using this with your vmfarm1 array:



          printf ' n|/%d.vmfarm1 ' "${#vmfarm1[@]}" >>textfile.txt





          share|improve this answer































            8














            To print the number of elements in an array variable in various shells with array support:





            • csh/tcsh/zsh/rc/es/akanga: echo $#array


            • ksh¹/bash¹/zsh: echo "${#array[@]}"


            • fish: count $array


            • yash: echo "${array[#]}"

            • Bourne/POSIX shells (where the only array is "$@"): echo "$#"


            Now for the number of whitespace delimited words in all the elements of an array variable, that's where you may want to use wc -w, but you'd need to feed it the content of all the elements separated by at least one white space for instance with:



            printf '%sn' $array:q | wc -w        # csh/tcsh
            printf '%sn' "${array[@]}" | wc -w # ksh/bash/zsh/yash
            printf '%sn' $array | wc -w # fish/zsh/rc/es/akanga
            printf '%sn' "$@" | wc -w # Bourne/POSIX


            Or you could do the splitting of the elements into further whitespace-delimited words and count them in the shell itself.





            • csh/tcsh (split on SPC/TAB/NL)



              (set noglob; set tmp=($array); echo $#tmp)



            • ksh/bash/yash ($IFS splitting, SPC/TAB/NL by default)



              (set -o noglob; set -- ${array[@]}; echo "$#")



            • zsh ($IFS splitting, SPC/TAB/NL/NUL by default)



              echo ${#${=array}}



            • rc/es ($ifs splitting):



              tmp = `{echo $array}
              echo $#tmp



            • fish (counts all sequences of non-whitespace (according to PCRE) characters):



              count (string match -ar -- 'S+' $array)



            • Bourne/POSIX ($IFS splitting):



              (set -f; set -- $@; echo "$#")





            ¹ note that given that ksh/bash arrays are sparse and have indices that start at 0 instead of 1 in every other shell, that number will generally not be the same as the maximum index in the array






            share|improve this answer































              3














              In Bash and ksh, expanding an array as if it was a normal string variable, gives the first element of the array. That is, $somearray is the same as ${somearray[0]}. (*)



              So,



              somearray=(foo bar doo)
              echo "$somearray"
              echo "$somearray" | wc -w


              prints foo and 1, since foo is only one word. If you had somearray=("foo bar doo" "acdc abba") instead, then the wc would show three words.



              You'll need to use "${somearray[@]}" to expand all elements of the array as distinct shell words (arguments), or "${somearray[*]}" to expand them as a single shell word, joined with spaces (**)



              In any case, note that the number of elements in an array, and the number of words (in the wc -w or the human language sense) are not the same, see below. Use "${#somearray[@]}" to get the number of elements in the array.



              somearray=("foo bar doo" "acdc abba")
              echo "${#somearray[@]}" # 2 elements, that contain
              echo "${somearray[@]}" | wc -w # 5 whitespace separated words in total




              (*) ignoring sparse and associative arrays for now.



              (**) assuming default IFS.






              share|improve this answer



















              • 1




                Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                – Stéphane Chazelas
                May 15 at 10:59



















              2














              vmfarm1, maximus and firefly are not just variables, these are arrays.



              Use the proper syntax: ${#vmfarm1[@]} is the number of entries in your array.






              share|improve this answer





























                0














                This may be not smart, but I think you can get number of hosts in array.



                vmfarm1=(host1.com host2.com host3.com host4.com)
                #maximus=(host11.com host12.com host13.com)
                #firefly(host5.com)
                COUNT=0

                for i in ${vmfarm1[@]};
                do
                HOSTCOUNT=`echo $i |wc -l`
                COUNT=$((COUNT + HOSTCOUNT))
                done

                printf "vmfarm1:%2dn" $COUNT


                Thanks






                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',
                  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%2f443891%2fnumber-of-elements-words-in-a-shell-array-variable%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  7














                  Given an array arr, its length (number of elements) is given by ${#arr[@]}.



                  Using this with your vmfarm1 array:



                  printf ' n|/%d.vmfarm1 ' "${#vmfarm1[@]}" >>textfile.txt





                  share|improve this answer




























                    7














                    Given an array arr, its length (number of elements) is given by ${#arr[@]}.



                    Using this with your vmfarm1 array:



                    printf ' n|/%d.vmfarm1 ' "${#vmfarm1[@]}" >>textfile.txt





                    share|improve this answer


























                      7












                      7








                      7






                      Given an array arr, its length (number of elements) is given by ${#arr[@]}.



                      Using this with your vmfarm1 array:



                      printf ' n|/%d.vmfarm1 ' "${#vmfarm1[@]}" >>textfile.txt





                      share|improve this answer














                      Given an array arr, its length (number of elements) is given by ${#arr[@]}.



                      Using this with your vmfarm1 array:



                      printf ' n|/%d.vmfarm1 ' "${#vmfarm1[@]}" >>textfile.txt






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 15 at 11:01

























                      answered May 15 at 10:44









                      Kusalananda

                      121k16228372




                      121k16228372

























                          8














                          To print the number of elements in an array variable in various shells with array support:





                          • csh/tcsh/zsh/rc/es/akanga: echo $#array


                          • ksh¹/bash¹/zsh: echo "${#array[@]}"


                          • fish: count $array


                          • yash: echo "${array[#]}"

                          • Bourne/POSIX shells (where the only array is "$@"): echo "$#"


                          Now for the number of whitespace delimited words in all the elements of an array variable, that's where you may want to use wc -w, but you'd need to feed it the content of all the elements separated by at least one white space for instance with:



                          printf '%sn' $array:q | wc -w        # csh/tcsh
                          printf '%sn' "${array[@]}" | wc -w # ksh/bash/zsh/yash
                          printf '%sn' $array | wc -w # fish/zsh/rc/es/akanga
                          printf '%sn' "$@" | wc -w # Bourne/POSIX


                          Or you could do the splitting of the elements into further whitespace-delimited words and count them in the shell itself.





                          • csh/tcsh (split on SPC/TAB/NL)



                            (set noglob; set tmp=($array); echo $#tmp)



                          • ksh/bash/yash ($IFS splitting, SPC/TAB/NL by default)



                            (set -o noglob; set -- ${array[@]}; echo "$#")



                          • zsh ($IFS splitting, SPC/TAB/NL/NUL by default)



                            echo ${#${=array}}



                          • rc/es ($ifs splitting):



                            tmp = `{echo $array}
                            echo $#tmp



                          • fish (counts all sequences of non-whitespace (according to PCRE) characters):



                            count (string match -ar -- 'S+' $array)



                          • Bourne/POSIX ($IFS splitting):



                            (set -f; set -- $@; echo "$#")





                          ¹ note that given that ksh/bash arrays are sparse and have indices that start at 0 instead of 1 in every other shell, that number will generally not be the same as the maximum index in the array






                          share|improve this answer




























                            8














                            To print the number of elements in an array variable in various shells with array support:





                            • csh/tcsh/zsh/rc/es/akanga: echo $#array


                            • ksh¹/bash¹/zsh: echo "${#array[@]}"


                            • fish: count $array


                            • yash: echo "${array[#]}"

                            • Bourne/POSIX shells (where the only array is "$@"): echo "$#"


                            Now for the number of whitespace delimited words in all the elements of an array variable, that's where you may want to use wc -w, but you'd need to feed it the content of all the elements separated by at least one white space for instance with:



                            printf '%sn' $array:q | wc -w        # csh/tcsh
                            printf '%sn' "${array[@]}" | wc -w # ksh/bash/zsh/yash
                            printf '%sn' $array | wc -w # fish/zsh/rc/es/akanga
                            printf '%sn' "$@" | wc -w # Bourne/POSIX


                            Or you could do the splitting of the elements into further whitespace-delimited words and count them in the shell itself.





                            • csh/tcsh (split on SPC/TAB/NL)



                              (set noglob; set tmp=($array); echo $#tmp)



                            • ksh/bash/yash ($IFS splitting, SPC/TAB/NL by default)



                              (set -o noglob; set -- ${array[@]}; echo "$#")



                            • zsh ($IFS splitting, SPC/TAB/NL/NUL by default)



                              echo ${#${=array}}



                            • rc/es ($ifs splitting):



                              tmp = `{echo $array}
                              echo $#tmp



                            • fish (counts all sequences of non-whitespace (according to PCRE) characters):



                              count (string match -ar -- 'S+' $array)



                            • Bourne/POSIX ($IFS splitting):



                              (set -f; set -- $@; echo "$#")





                            ¹ note that given that ksh/bash arrays are sparse and have indices that start at 0 instead of 1 in every other shell, that number will generally not be the same as the maximum index in the array






                            share|improve this answer


























                              8












                              8








                              8






                              To print the number of elements in an array variable in various shells with array support:





                              • csh/tcsh/zsh/rc/es/akanga: echo $#array


                              • ksh¹/bash¹/zsh: echo "${#array[@]}"


                              • fish: count $array


                              • yash: echo "${array[#]}"

                              • Bourne/POSIX shells (where the only array is "$@"): echo "$#"


                              Now for the number of whitespace delimited words in all the elements of an array variable, that's where you may want to use wc -w, but you'd need to feed it the content of all the elements separated by at least one white space for instance with:



                              printf '%sn' $array:q | wc -w        # csh/tcsh
                              printf '%sn' "${array[@]}" | wc -w # ksh/bash/zsh/yash
                              printf '%sn' $array | wc -w # fish/zsh/rc/es/akanga
                              printf '%sn' "$@" | wc -w # Bourne/POSIX


                              Or you could do the splitting of the elements into further whitespace-delimited words and count them in the shell itself.





                              • csh/tcsh (split on SPC/TAB/NL)



                                (set noglob; set tmp=($array); echo $#tmp)



                              • ksh/bash/yash ($IFS splitting, SPC/TAB/NL by default)



                                (set -o noglob; set -- ${array[@]}; echo "$#")



                              • zsh ($IFS splitting, SPC/TAB/NL/NUL by default)



                                echo ${#${=array}}



                              • rc/es ($ifs splitting):



                                tmp = `{echo $array}
                                echo $#tmp



                              • fish (counts all sequences of non-whitespace (according to PCRE) characters):



                                count (string match -ar -- 'S+' $array)



                              • Bourne/POSIX ($IFS splitting):



                                (set -f; set -- $@; echo "$#")





                              ¹ note that given that ksh/bash arrays are sparse and have indices that start at 0 instead of 1 in every other shell, that number will generally not be the same as the maximum index in the array






                              share|improve this answer














                              To print the number of elements in an array variable in various shells with array support:





                              • csh/tcsh/zsh/rc/es/akanga: echo $#array


                              • ksh¹/bash¹/zsh: echo "${#array[@]}"


                              • fish: count $array


                              • yash: echo "${array[#]}"

                              • Bourne/POSIX shells (where the only array is "$@"): echo "$#"


                              Now for the number of whitespace delimited words in all the elements of an array variable, that's where you may want to use wc -w, but you'd need to feed it the content of all the elements separated by at least one white space for instance with:



                              printf '%sn' $array:q | wc -w        # csh/tcsh
                              printf '%sn' "${array[@]}" | wc -w # ksh/bash/zsh/yash
                              printf '%sn' $array | wc -w # fish/zsh/rc/es/akanga
                              printf '%sn' "$@" | wc -w # Bourne/POSIX


                              Or you could do the splitting of the elements into further whitespace-delimited words and count them in the shell itself.





                              • csh/tcsh (split on SPC/TAB/NL)



                                (set noglob; set tmp=($array); echo $#tmp)



                              • ksh/bash/yash ($IFS splitting, SPC/TAB/NL by default)



                                (set -o noglob; set -- ${array[@]}; echo "$#")



                              • zsh ($IFS splitting, SPC/TAB/NL/NUL by default)



                                echo ${#${=array}}



                              • rc/es ($ifs splitting):



                                tmp = `{echo $array}
                                echo $#tmp



                              • fish (counts all sequences of non-whitespace (according to PCRE) characters):



                                count (string match -ar -- 'S+' $array)



                              • Bourne/POSIX ($IFS splitting):



                                (set -f; set -- $@; echo "$#")





                              ¹ note that given that ksh/bash arrays are sparse and have indices that start at 0 instead of 1 in every other shell, that number will generally not be the same as the maximum index in the array







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 11 at 10:07

























                              answered May 15 at 10:48









                              Stéphane Chazelas

                              298k54563910




                              298k54563910























                                  3














                                  In Bash and ksh, expanding an array as if it was a normal string variable, gives the first element of the array. That is, $somearray is the same as ${somearray[0]}. (*)



                                  So,



                                  somearray=(foo bar doo)
                                  echo "$somearray"
                                  echo "$somearray" | wc -w


                                  prints foo and 1, since foo is only one word. If you had somearray=("foo bar doo" "acdc abba") instead, then the wc would show three words.



                                  You'll need to use "${somearray[@]}" to expand all elements of the array as distinct shell words (arguments), or "${somearray[*]}" to expand them as a single shell word, joined with spaces (**)



                                  In any case, note that the number of elements in an array, and the number of words (in the wc -w or the human language sense) are not the same, see below. Use "${#somearray[@]}" to get the number of elements in the array.



                                  somearray=("foo bar doo" "acdc abba")
                                  echo "${#somearray[@]}" # 2 elements, that contain
                                  echo "${somearray[@]}" | wc -w # 5 whitespace separated words in total




                                  (*) ignoring sparse and associative arrays for now.



                                  (**) assuming default IFS.






                                  share|improve this answer



















                                  • 1




                                    Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                    – Stéphane Chazelas
                                    May 15 at 10:59
















                                  3














                                  In Bash and ksh, expanding an array as if it was a normal string variable, gives the first element of the array. That is, $somearray is the same as ${somearray[0]}. (*)



                                  So,



                                  somearray=(foo bar doo)
                                  echo "$somearray"
                                  echo "$somearray" | wc -w


                                  prints foo and 1, since foo is only one word. If you had somearray=("foo bar doo" "acdc abba") instead, then the wc would show three words.



                                  You'll need to use "${somearray[@]}" to expand all elements of the array as distinct shell words (arguments), or "${somearray[*]}" to expand them as a single shell word, joined with spaces (**)



                                  In any case, note that the number of elements in an array, and the number of words (in the wc -w or the human language sense) are not the same, see below. Use "${#somearray[@]}" to get the number of elements in the array.



                                  somearray=("foo bar doo" "acdc abba")
                                  echo "${#somearray[@]}" # 2 elements, that contain
                                  echo "${somearray[@]}" | wc -w # 5 whitespace separated words in total




                                  (*) ignoring sparse and associative arrays for now.



                                  (**) assuming default IFS.






                                  share|improve this answer



















                                  • 1




                                    Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                    – Stéphane Chazelas
                                    May 15 at 10:59














                                  3












                                  3








                                  3






                                  In Bash and ksh, expanding an array as if it was a normal string variable, gives the first element of the array. That is, $somearray is the same as ${somearray[0]}. (*)



                                  So,



                                  somearray=(foo bar doo)
                                  echo "$somearray"
                                  echo "$somearray" | wc -w


                                  prints foo and 1, since foo is only one word. If you had somearray=("foo bar doo" "acdc abba") instead, then the wc would show three words.



                                  You'll need to use "${somearray[@]}" to expand all elements of the array as distinct shell words (arguments), or "${somearray[*]}" to expand them as a single shell word, joined with spaces (**)



                                  In any case, note that the number of elements in an array, and the number of words (in the wc -w or the human language sense) are not the same, see below. Use "${#somearray[@]}" to get the number of elements in the array.



                                  somearray=("foo bar doo" "acdc abba")
                                  echo "${#somearray[@]}" # 2 elements, that contain
                                  echo "${somearray[@]}" | wc -w # 5 whitespace separated words in total




                                  (*) ignoring sparse and associative arrays for now.



                                  (**) assuming default IFS.






                                  share|improve this answer














                                  In Bash and ksh, expanding an array as if it was a normal string variable, gives the first element of the array. That is, $somearray is the same as ${somearray[0]}. (*)



                                  So,



                                  somearray=(foo bar doo)
                                  echo "$somearray"
                                  echo "$somearray" | wc -w


                                  prints foo and 1, since foo is only one word. If you had somearray=("foo bar doo" "acdc abba") instead, then the wc would show three words.



                                  You'll need to use "${somearray[@]}" to expand all elements of the array as distinct shell words (arguments), or "${somearray[*]}" to expand them as a single shell word, joined with spaces (**)



                                  In any case, note that the number of elements in an array, and the number of words (in the wc -w or the human language sense) are not the same, see below. Use "${#somearray[@]}" to get the number of elements in the array.



                                  somearray=("foo bar doo" "acdc abba")
                                  echo "${#somearray[@]}" # 2 elements, that contain
                                  echo "${somearray[@]}" | wc -w # 5 whitespace separated words in total




                                  (*) ignoring sparse and associative arrays for now.



                                  (**) assuming default IFS.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited May 15 at 11:00

























                                  answered May 15 at 10:56









                                  ilkkachu

                                  55.4k782150




                                  55.4k782150








                                  • 1




                                    Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                    – Stéphane Chazelas
                                    May 15 at 10:59














                                  • 1




                                    Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                    – Stéphane Chazelas
                                    May 15 at 10:59








                                  1




                                  1




                                  Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                  – Stéphane Chazelas
                                  May 15 at 10:59




                                  Beware that first element of an array is ambiguous with ksh and bash as their arrays are space. $array expands to ${array[0]} or the empty string if the array has no element of index 0. For the first element of the array, you actually need ${array[@]:0:1}.
                                  – Stéphane Chazelas
                                  May 15 at 10:59











                                  2














                                  vmfarm1, maximus and firefly are not just variables, these are arrays.



                                  Use the proper syntax: ${#vmfarm1[@]} is the number of entries in your array.






                                  share|improve this answer


























                                    2














                                    vmfarm1, maximus and firefly are not just variables, these are arrays.



                                    Use the proper syntax: ${#vmfarm1[@]} is the number of entries in your array.






                                    share|improve this answer
























                                      2












                                      2








                                      2






                                      vmfarm1, maximus and firefly are not just variables, these are arrays.



                                      Use the proper syntax: ${#vmfarm1[@]} is the number of entries in your array.






                                      share|improve this answer












                                      vmfarm1, maximus and firefly are not just variables, these are arrays.



                                      Use the proper syntax: ${#vmfarm1[@]} is the number of entries in your array.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 15 at 10:45









                                      emmrk

                                      19913




                                      19913























                                          0














                                          This may be not smart, but I think you can get number of hosts in array.



                                          vmfarm1=(host1.com host2.com host3.com host4.com)
                                          #maximus=(host11.com host12.com host13.com)
                                          #firefly(host5.com)
                                          COUNT=0

                                          for i in ${vmfarm1[@]};
                                          do
                                          HOSTCOUNT=`echo $i |wc -l`
                                          COUNT=$((COUNT + HOSTCOUNT))
                                          done

                                          printf "vmfarm1:%2dn" $COUNT


                                          Thanks






                                          share|improve this answer




























                                            0














                                            This may be not smart, but I think you can get number of hosts in array.



                                            vmfarm1=(host1.com host2.com host3.com host4.com)
                                            #maximus=(host11.com host12.com host13.com)
                                            #firefly(host5.com)
                                            COUNT=0

                                            for i in ${vmfarm1[@]};
                                            do
                                            HOSTCOUNT=`echo $i |wc -l`
                                            COUNT=$((COUNT + HOSTCOUNT))
                                            done

                                            printf "vmfarm1:%2dn" $COUNT


                                            Thanks






                                            share|improve this answer


























                                              0












                                              0








                                              0






                                              This may be not smart, but I think you can get number of hosts in array.



                                              vmfarm1=(host1.com host2.com host3.com host4.com)
                                              #maximus=(host11.com host12.com host13.com)
                                              #firefly(host5.com)
                                              COUNT=0

                                              for i in ${vmfarm1[@]};
                                              do
                                              HOSTCOUNT=`echo $i |wc -l`
                                              COUNT=$((COUNT + HOSTCOUNT))
                                              done

                                              printf "vmfarm1:%2dn" $COUNT


                                              Thanks






                                              share|improve this answer














                                              This may be not smart, but I think you can get number of hosts in array.



                                              vmfarm1=(host1.com host2.com host3.com host4.com)
                                              #maximus=(host11.com host12.com host13.com)
                                              #firefly(host5.com)
                                              COUNT=0

                                              for i in ${vmfarm1[@]};
                                              do
                                              HOSTCOUNT=`echo $i |wc -l`
                                              COUNT=$((COUNT + HOSTCOUNT))
                                              done

                                              printf "vmfarm1:%2dn" $COUNT


                                              Thanks







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 15 at 14:59









                                              Kusalananda

                                              121k16228372




                                              121k16228372










                                              answered May 15 at 14:51









                                              H.Abe

                                              1




                                              1






























                                                  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%2f443891%2fnumber-of-elements-words-in-a-shell-array-variable%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

                                                  Morgemoulin

                                                  Scott Moir

                                                  Souastre