Bash Built-In Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 39: Line 39:
===IFS and for===
===IFS and for===


<tt>for</tt> honors the value of IFS (default the space). If you set IFS to something else, before the <tt>for</tt> statement, <tt>for</tt> will use that as field separator while iterating over the list. For more details see [[bash_for#for_and_IFS|<tt>for</tt> and <tt>IFS</tt>]]
<tt>for</tt> honors the value of IFS (default the space). If you set IFS to something else, before the <tt>for</tt> statement, <tt>for</tt> will use that as field separator while iterating over the list.
 
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:'''Note''' be extremely careful when setting IFS before a <tt>for</tt> loop, even if you restore the default value after the loop: everything inside the loop will use the non-standard IFS value and it may not work as expected.<br>
</blockquote>
 
 
For more details see [[bash_for#for_and_IFS|<tt>for</tt> and <tt>IFS</tt>]]


Related:
Related:

Revision as of 04:02, 6 March 2016

External

Internal

Overview

Environment Variable Hierarchy

When a parent shell forks a new sub-shell, the sub-shell process has a copy of the parent's environment and no access to the parent process's environment whatsoever. When the shell sub-process terminates any changes made to its environment are lost. There is no way to modify directly the parent's environment.

Standard Environment Variables

IFS

IFS is the internal field separator. This variable determines how bash recognizes fields (word boundaries) when it interprets character strings. IFS defaults to whitespace (space, tab and newline). This is the proof:

echo "$IFS" | cat -vte
 ^I$
$

IFS can be changed.

Note you must set IFS back to whitespace after setting it to something else, so the basic shell function work as expected. This is done as shown below: restoring the default IFS value.

Restoring the default IFS value

IFS="$(printf ' \t\n')"

IFS and for

for honors the value of IFS (default the space). If you set IFS to something else, before the for statement, for will use that as field separator while iterating over the list.

Note be extremely careful when setting IFS before a for loop, even if you restore the default value after the loop: everything inside the loop will use the non-standard IFS value and it may not work as expected.


For more details see for and IFS

Related:

bash set List Separator
for and IFS