[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: read builtin. input processes improperly inheriting IFS setting
From: |
Greg Wooledge |
Subject: |
Re: read builtin. input processes improperly inheriting IFS setting |
Date: |
Fri, 26 Jul 2013 15:25:42 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Sat, Jul 27, 2013 at 01:34:53AM +0900, David H. wrote:
> # The test string:
> $ echo $instring
> root:x:0:0:root:/root:/bin/bash
>
> # Gives incorrect (unexpected) output:
> $ ( IFS=: read -a strings < <( echo $instring ) ; printf '[%s]\n'
> "${strings[@]}" )
> [root x 0 0 root /root /bin/bash]
You must quote "$instring" to prevent word splitting by the shell.
IFS=: read -a strings < <(echo "$instring")
or
IFS=: read -a strings <<< "$instring"
Your unquoted $instring is being split into fields by bash, which is
operating with IFS=: at that point. Quoting it avoids this problem.
imadev:~$ instring="root:x:0:0:root:/root:/bin/bash"
imadev:~$ IFS=: read -a strings < <(echo "$instring")
imadev:~$ printf '<%s> ' "${strings[@]}"; echo
<root> <x> <0> <0> <root> </root> </bin/bash>