| Start | ||||||
|---|---|---|---|---|---|---|
Bad effects of word splitting
See what, when and why (opens new window) for the basics. |
||||||
| Splitting with IFS To be more clear, let's create a script named Now let's see the differences:
Now let's set
|
||||||
| What, when and Why? When the shell performs parameter expansion, command substitution, variable or arithmetic expansion, it scans for word boundaries in the result. If any word boundary is found, then the result is split into multiple words at that position. The word boundary is defined by a shell variable In the above example this is how the
|
||||||
| IFS & word splitting See what, when and why (opens new window) if you don't know about the affiliation of IFS to word splitting let's set the IFS to space character only: This time word splitting will only work on spaces. The
Let's set the IFS to newline only: Now the
Let's see what happens if we set IFS to nullstring: This time the
You can prevent word splitting by setting the IFS to nullstring A general way of preventing word splitting is to use double quote: will prevent word splitting in all the cases discussed above i.e the |
||||||
| Usefulness of word splitting There are some cases where word splitting can be useful: Filling up array:
Looping through space separated words: Output: Passing space separated parameters which don't contain white spaces: or
See what, when and why (opens new window) for the basics. |
||||||
| Splitting by separator changes We can just do simple replacement of separators from space to new line, as following example. It'll split the value of the variable Syntax
Parameters
Remarks
|
Read Article
https://develop.horje.com/learn/1434/reference