\n' for i in `cat...">

When should we change the IFS variable to its original value in scripts?

Let's say I execute this script through cronjob:

IFS=$'\n' for i in `cat "$1"`; do echo "$i" >> xtempfile.tmp; done 

It works great without causing any problems. But when I run this in the terminal, I need to set the IFS variable back to its original value

 IFS=$OLDIFS 

Generally, in what cases do we need to set IFS back to its original value?

+4
source share
1 answer

Instead:

 IFS=$'\n' for i in `cat "$1"`; do echo "$i" >> xtempfile.tmp; done 

You can do something like:

 while IFS=$'\n' read -r line; do echo "$line" done < "$1" >> xtempfile.tmp 

This would set the IFS variable during the while loop .

* Add-on based on samveen comments. Anytime a change in IFS occurs in a subshell, the changes are automatically returned. However, this is not the case when you made changes to the interactive shell. The above sentence is the required processing so that we do not accidentally change the IFS for the entire shell. *

+6
source

Source: https://habr.com/ru/post/1486961/


All Articles