Splitting with IFS
suggest changeTo be more clear, let’s create a script named showarg:
#!/usr/bin/env bash
printf "%d args:" $#
printf " <%s>" "$@"
echo
Now let’s see the differences:
$ var="This is an example"
$ showarg $var
4 args: <This> <is> <an> <example>
$varis split into 4 arguments. The default IFS is whitespace characters, so word splitting occurred at the spaces.
$ var="This/is/an/example"
$ showarg $var
1 args: <This/is/an/example>
In the example above, word splitting didn’t occur because the IFS characters weren’t found.
Now let’s set IFS=/
$ IFS=/
$ var="This/is/an/example"
$ showarg $var
4 args: <This> <is> <an> <example>
$varis split into 4 arguments, not a single argument.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents