ergbil ergbil - 7 months ago 13
Bash Question

What is the Exact Use and Meaning of "IFS=!"

I was trying to understand the usage of IFS but there is something I couldn't find any information about.

My example code:

#!/bin/sh
# (C) 2016 Ergin Bilgin

IFS=!
for LINE in $(last -a | sed '$ d')
do
echo $LINE | awk '{print $1}'
done
unset IFS


I use this code to print last users line by line. I totally understand the usage of IFS and in this example when I use default IFS, it reads word by word inside of my loop. And when I use
IFS=!
it reads line by line as I wish. The problem here is I couldn't find anything about that "!" on anywhere. I don't remember where I learned that. When I google about achieving same kind of behaviour, I see other values which are usually strings.

So, what is the meaning of that "!" and how it gives me the result I wish?

Thanks

Answer

IFS=! is merely setting a non-existent value for IFS so that you can iterate input line by line. Having said that using for loop here is not recommended, better to use read in a while loop like this to print first column i.e. username:

last | sed '$ d' | while read -r u _; do
   echo "$u"
done