Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Customizing bash's Internal Field Separator (IFS)

  1. #1
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Customizing bash's Internal Field Separator (IFS)

    I'm running into some weirdness trying to explicitly set the special variable in bash, the Internal Field Separator to be a space, a new line and a tab. This is in GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)

    I would like to be able to explicitly set the Internal Field Separator to be a space, a new line and a tab. I would expect the backslash sequences to be interpreted, but they seem not to be. Despite many HOWTOs, Tutorials, and guides only setup 'one' below successfully assigns all three.

    # one
    export IFS=`/bin/echo -ne " \t\n"`
    echo "$IFS" | cat -vte

    These others below fail, usually by making the letters 't' and 'n' into field separators. That is not the behavior I expect, I would expect the backslash sequences to be interpreted properly:

    # two
    export IFS=" \t\n"
    echo "$IFS" | cat -vte

    # three
    export IFS=$" \t\n"
    echo "$IFS" | cat -vte

    # four
    export IFS=$' \t\n'
    echo "$IFS" | cat -vte

    Here is the problem in a different form:

    $ FOO=$'a\n a';echo $FOO
    a a
    $ FOO=$"a\n a";echo $FOO
    a\n a
    $ FOO="a\n a";echo $FOO
    a\n a
    $ FOO='a\n a';echo $FOO
    a\n a
    Last edited by Lars Noodén; June 6th, 2008 at 12:42 PM.

  2. #2
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Customizing bash's Internal Field Separator (IFS)

    Seems to be working for me.
    Code:
    $ IFS=$' \t\n' FOO=$'a b\tc\nd-e'; echo $FOO
    a b c d-e
    $ IFS=' ' FOO=$'a b\tc\nd-e'; echo $FOO
    a b	c
    d-e
    $ IFS=$'\t' FOO=$'a b\tc\nd-e'; echo $FOO
    a b c
    d-e
    $ IFS=b FOO=$'a b\tc\nd-e'; echo $FOO
    a  	c
    d-e

  3. #3
    Join Date
    Apr 2005
    Location
    Austria
    Beans
    246
    Distro
    Kubuntu

    Re: Customizing bash's Internal Field Separator (IFS)

    Also note that bash's default IFS already consists of " \t\n".
    johannes.truschnigg.info - meine Website.

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    which version and platform?

    @geirha: on which platform and for which version of bash is it working for you?

    I am finding that it does not work on

    GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)

    2.6.24-18-generic #1 SMP Wed May 28 19:28:38 UTC 2008 x86_64 GNU/Linux

    $ lsb_release -rcd
    Description: Ubuntu 8.04
    Release: 8.04
    Codename: hardy

  5. #5
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Customizing bash's Internal Field Separator (IFS)

    Quote Originally Posted by colo View Post
    Also note that bash's default IFS already consists of " \t\n".
    Yes. However, for scripts, I wish to explicitly set the IFS, but without having to backtick output from echo.

  6. #6
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: which version and platform?

    Quote Originally Posted by Lars Noodén View Post
    @geirha: on which platform and for which version of bash is it working for you?

    I am finding that it does not work on

    GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)

    2.6.24-18-generic #1 SMP Wed May 28 19:28:38 UTC 2008 x86_64 GNU/Linux

    $ lsb_release -rcd
    Description: Ubuntu 8.04
    Release: 8.04
    Codename: hardy
    GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

    2.6.24-18-386 #1 Wed May 28 19:30:01 UTC 2008 i686 GNU/Linux

    Ubuntu 8.04

  7. #7
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Works on i686

    Quote Originally Posted by geirha View Post
    GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

    2.6.24-18-386 #1 Wed May 28 19:30:01 UTC 2008 i686 GNU/Linux

    Ubuntu 8.04
    Hmm. I'm finding that it does work for a different architecture:

    $ uname -a ;lsb_release -rc
    Linux oin 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686 GNU/Linux
    Release: 7.10
    Codename: gutsy
    maintenance@oin:~$ uname -a ;lsb_release -rc;bash --version
    Linux oin 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686 GNU/Linux
    Release: 7.10
    Codename: gutsy
    GNU bash, version 3.2.25(1)-release (i486-pc-linux-gnu)
    Copyright (C) 2005 Free Software Foundation, Inc.

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Problem is in the shell script: sh == dash

    Thanks for helping to walk through the problem.

    The failure to interpret backslash escapes happens when starting the shell script with just sh
    #!/bin/sh

    /bin/sh is really dash, not bash
    https://launchpad.net/ubuntu/+spec/dash-as-bin-sh

    So, changing the first line to the following gets rid of the problem:
    #!/bin/bash


    I guess that not interpreting backslash escapes is supposed to be normal behavior for dash?

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Customizing bash's Internal Field Separator (IFS)

    for hassle free delimiter parsing, use awk.

  10. #10
    Join Date
    May 2008
    Location
    Eugene, OR, USA
    Beans
    435

    Re: Customizing bash's Internal Field Separator (IFS)

    Btw, this syntax works:

    Code:
    #!/bin/bash
    
    IFS=$' \t\n'
    Hal Pomeranz, Deer Run Associates
    [[ Various Linux/Unix related documents ]]
    [[ Command-Line Kung Fu blog ]]

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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