2

I am trying to understand a test script, which includes the following segment:

OIFS=$IFS;
IFS="|";
nandal
  • 2,544
  • 1
  • 18
  • 23
Samarth R
  • 51
  • 1
  • 5
  • `IFS` is the internal field separator; see e.g. [this question](https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash) with its accepted answer. `OIFS` just stores the `O`ld value, so it's easy to reset it. – 9769953 Jun 26 '18 at 11:17

3 Answers3

8

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.

Sam
  • 811
  • 5
  • 9
3

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
apatniv
  • 1,771
  • 10
  • 13
0

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="|";
nandal
  • 2,544
  • 1
  • 18
  • 23