Find the answer to your Linux question:
Results 1 to 3 of 3
When I try to iterate over some filenames (with whitespaces in them) in bash, I came up with the following: export IFS=$(echo -e "\n") for i in $(ls -1); do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    4

    newline separator in bash for loop


    When I try to iterate over some filenames (with whitespaces in them) in bash, I came up with the following:

    export IFS=$(echo -e "\n")
    for i in $(ls -1); do echo $i; done;

    however, this does not seperate the files at all; it stores all the filenames in one variable $i.

    When I change IFS to:
    export IFS=$(echo -e "\n\b")
    it does give the desirable response.

    Why is the '\b' necessary? What needs to be 'backspaced' ?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I think the \b prevents the newline character from getting ignored by the shell, but you can instead try this cleaner alternative:

    Code:
    IFS=$'\n'

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    4
    That seems like a much cleaner way indeed. Thanks for the help!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •