Search code examples
bashcsvvariablesawkspace

Why does using using awk work fine, but if i assign a variable to said awk command it breaks anything that has a space in it?


I'm making a script that reads a CSV where I have placed the locations of various icons I want to put on a dock. If I run

awk -F "," '{print $1} test.csv

I will get:

/Applications/Launchpad  
/Applications/Safari   
/Applications/Pages  
/Applications/Numbers  
/Applications/Keynote  
/Applications/Photos   
/Applications/iMovie  
/Applications/GarageBand  
/Applications/Microsoft\ Word  
/Applications/Microsoft\ Excel  
/Applications/Microsoft\ Powerpoint  

But if i assign that same command to variable this and run

 for i in $this; do
     echo $i;

the last three lines print like this:

/Applications/Microsoft\  
Word  
/Applications/Microsoft\  
Excel  
/Applications/Microsoft\  
Powerpoint  

I understand that it's obviously breaking at the space and will need to fix it, but I don't understand why it works in one but not the other. strong text


Solution

  • By default, a Bash for loop splits on all whitespace, you can override it by setting IFS=$'\n' before loop

    Test Results:

    akshay@db-3325:/tmp$ cat testfile 
    /Applications/Launchpad  
    /Applications/Safari   
    /Applications/Pages  
    /Applications/Numbers  
    /Applications/Keynote  
    /Applications/Photos   
    /Applications/iMovie  
    /Applications/GarageBand  
    /Applications/Microsoft\ Word  
    /Applications/Microsoft\ Excel  
    /Applications/Microsoft\ Powerpoint  
    

    say you store your results using awk something like below

    akshay@db-3325:/tmp$ testvar=$(awk '1' testfile)
    

    Default behaviour

    akshay@db-3325:/tmp$ for i in $testvar; do echo "$i"; done  
    /Applications/Launchpad
    /Applications/Safari
    /Applications/Pages
    /Applications/Numbers
    /Applications/Keynote
    /Applications/Photos
    /Applications/iMovie
    /Applications/GarageBand
    /Applications/Microsoft\
    Word
    /Applications/Microsoft\
    Excel
    /Applications/Microsoft\
    Powerpoint
    

    this is with IFS

    akshay@db-3325:/tmp$ IFS=$'\n';for i in $testvar; do echo "$i"; done; 
    /Applications/Launchpad  
    /Applications/Safari   
    /Applications/Pages  
    /Applications/Numbers  
    /Applications/Keynote  
    /Applications/Photos   
    /Applications/iMovie  
    /Applications/GarageBand  
    /Applications/Microsoft\ Word  
    /Applications/Microsoft\ Excel  
    /Applications/Microsoft\ Powerpoint