Tutorial by Examples

To 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> <exa...
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 IFS (Interna...
See what, when and why if you don't know about the affiliation of IFS to word splitting let's set the IFS to space character only: set -x var='I am a multiline string' IFS=' ' fun() { echo "-$1-" echo "*$2*" echo ".$3." } fun $var This time wo...
$ a='I am a string with spaces' $ [ $a = $a ] || echo "didn't match" bash: [: too many arguments didn't match [ $a = $a ] was interpreted as [ I am a string with spaces = I am a string with spaces ]. [ is the test command for which I am a string with spaces is not a single argument...
There are some cases where word splitting can be useful: Filling up array: arr=($(grep -o '[0-9]\+' file)) This will fill up arr with all numeric values found in file Looping through space separated words: words='foo bar baz' for w in $words;do echo "W: $w" done Output...
We can just do simple replacement of separators from space to new line, as following example. echo $sentence | tr " " "\n" It'll split the value of the variable sentence and show it line by line respectively.

Page 1 of 1