How to set IFS for a specific command

Hi there,
I'm using two commands that need different IFS.

The mysql command need IFS to include space because I'm given the mysql command as a variable:

supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
$MYSQL -Ne "SELECT COUNT(1), MAX(id), MAX(name) FROM terminal"
supernova:~# bash myscript
+---+-------------+---------+
| 8 | wide screen | library |
+---+-------------+---------+

If IFS doesn't include space, then $MYSQL is considered as one word and not command + switch:

supernova:~# cat myscript
IFS=
MYSQL="mysql -u user -ppassword database"
$MYSQL -Ne "SELECT COUNT(1), MAX(id), MAX(name) FROM terminal"
supernova:~# bash myscript
myscrypt: line 3: mysql -u user -ppassword database: command not found

The read command needs IFS to include tab and exclude space:

supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t")
read a b c <<< "$result"
echo $a-$b-$c
supernova:~# bash myscript
8-wide screen-library

If IFS include space, then mysql output is not correctly interpreted:

supernova:~# cat myscript
IFS=' '
MYSQL="mysql -u user -ppassword database"
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t ")
read a b c <<< "$result"
echo $a-$b-$c
supernova:~# bash myscript
8-wide-screen library

I have to run this kind of queries many time in the script and thought it's a pain in the ass to set IFS two times every time I run a query.
Is there any way I can write something like:

read a b c <<< "$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")"

Instead of:

IFS=' '
result=$($MYSQL -Ne "SELECT COUNT(1), MAX(name), MAX(space) FROM terminal")
IFS=$(echo -e "\t")
read a b c <<< "$result"

I got lost in your post, but in general I leave IFS alone so it has its default global value. Then if I need a special setting, I set it for that one command:

while IFS="" read line ; do

as one example. There the read statement gets its own IFS setting, but everything else gets the default. You just put the IFS value in front on the command.

Well, I think I found something.
Don't really know why it works, just tried several possibilities.

MYSQL="mysql -u user -ppassword database"
TAB=$(echo -e "\t")

{ IFS="$TAB" read a b c; } <<< "$($MYSQL -Ne "SELECT id, nom, is_master FROM terminal WHERE id = 4")"
echo "$a-$b-$c"

{ IFS="$TAB" read a b c; } <<< "$($MYSQL -Ne "SELECT id, nom, is_master FROM terminal WHERE id = 14")"
echo "$a-$b-$c"

Thanks Perderabo for your help.
I knew this trick for while read and that was the bases of my attempts.
Could you then explain me why I have to put the statement between braces?

Update: If I don't put the braces, IFS is modified for the entire script.

You should not need the braces and I cannot explain why you do. I'll try it tomorrow. The bash man page states:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described above in PARAMETERS. These assignment statements affect only the environment seen by that command.

The behavior you report violates that last sentence. :confused:

I find that some shells(bash) treat IFS specially, overwriting it when you wouldn't expect it to.

One more reason for me to stay with ksh. I do that all the time with ksh and I never observe unexpected behavior involving IFS. I'll try it with bash tomorrow and see what happens.

Bash does not overwrite IFS, but it does always set it to the default in a new shell. It does not take its value from the environment. If you want something other than the default in a shell script, you must set it explicitly. (This is the same behaviour as ksh.)