ergbil ergbil - 2 years ago 72
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:

# (C) 2016 Ergin Bilgin

for LINE in $(last -a | sed '$ d')
echo $LINE | awk '{print $1}'
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
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?


Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download