I am trying to understand a test script, which includes the following segment:
OIFS=$IFS;
IFS="|";
I am trying to understand a test script, which includes the following segment:
OIFS=$IFS;
IFS="|";
OIFS here is a user defined variable that serves to backup the current Bash internal field separator value.
Then the internal field seperator variable is set to a user defined value, likely to enable some sort of parsing / text processing algorithm that relies on it and to be restored to its original value somewhere later in the script.
IFS is internal field separator. The snippet is changing the IFS to "|" after saving old value so the it can be restored later.
Example:
->array=(one two three)
->echo "${array[*]}"
one two three
->IFS='|'
->echo "${array[*]}"
one|two|three
In shell whenever we need to access the value of a variable, we use $variableName
and whenever we need to assign some value to a variable we use variableName=xxx
.
Therefore:-
# here we assigning the value of $IFS(Internal Field Separator) in OIFS.
OIFS=$IFS;
# and here we are re-assigning some different value to IFS.
# it's more like, first we store old value of IFS variable and then assign new value to it. So that later we can use the old value if needed.
IFS="|";