1

Summary

Depending on what bash or awk version I have, I get different results when I try to slice an array that holds awk output. On older versions of bash and awk the "slice operation" ( e.g. ${a[@]:2}) incorrectly returns an array with a single element regardless of how many elements are left in the sliced array. However, this only seems to be an issue when slicing arrays that "capture" awk output.

MWE

  • You can run the following to reproduce the issue:

      bash array_slice.sh text.file
    

    where array_slice.sh and text.file are the following:

  • array_slice.sh script:

       #!/usr/bin/env bash                                                        
    
       defaultIFS=$IFS                                                            
       IFS=$'\n'                                                                  
       t=($(awk '{ printf("%s\n", $0) }' "$1"))                                   
       printf "\nArray 't' has ${#t[@]} elements\n"                               
       printf "Elements of array 't':\n"                                          
       printf "%s\n" "${t[@]}"                                                    
    
       t45=( ${t[@]:4} )                                                          
       printf "\nArray 't45' has ${#t45[@]} elements\n"                           
       printf "Elements of array 't45':\n"                                        
       printf "%s\n" "${t45[@]}"      
    
  • text.file :

      some text                                                                   
      some other text                                                             
      this is text too                                                            
      text above is text                                                          
      and text below is text text                                                 
      text text  
    

Output for various platforms

Notice how array t45 only has one element in Ubuntu 16.04's output (it's supposed to have two) and how it correctly has two elements in Ubuntu 20.04's output.

  • Ubuntu 16.04 with

    • GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
    • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu) Gives:
      Array 't' has 6 elements
      Elements of array 't':
      some text
      some other text
      this is text too
      text above is text
      and text below is text text
      text text
    
      Array 't45' has 1 elements
      Elements of array 't45':
      and text below is text text text text
    
  • Ubuntu 20.04 with

    • GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
    • GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu) Gives:
    Array 't' has 6 elements
    Elements of array 't':
    some text
    some other text
    this is text too
    text above is text
    and text below is text text
    text text
    
    Array 't45' has 2 elements
    Elements of array 't45':
    and text below is text text
    text text
    
2
  • 1
    Please confirm you don't want t45=( "${t[@]:4}" ). If you want to build an array from exact elements of another array then you should quote. If you want to create a string that possibly gets split again then don't quote. I believe you there's a difference when you don't quote. I cannot fully explain this behavior yet. Maybe you want to research the phenomenon for educational reasons or whatever. But maybe you observed the behavior in circumstances where the right thing is to double-quote in the first place. Mar 5, 2021 at 23:28
  • Quoting works on the MWE. I've checked on Ubuntu (16.04, 20.04), Windows, and MacOS (though on MacOS I needed to install a new bash). It seems that with really old bash versions, such as the default MacOS one, even the quoting doesn't work. However, I believe the quoting solution will work where in the use case that I need to solve.
    – gomfy
    Mar 5, 2021 at 23:49

0

You must log in to answer this question.

Browse other questions tagged .