Debian Codegraph @

ifs.sh

raw symbols SCIP
1 #!/bin/bash
2 # ifs.sh
3
4
5 var1="a+b+c"
6 var2="d-e-f"
7 var3="g,h,i"
8
9 IFS=+
10 # The plus sign will be interpreted as a separator.
11 echo $var1 # a b c
12 echo $var2 # d-e-f
13 echo $var3 # g,h,i
14
15 echo
16
17 IFS="-"
18 # The plus sign reverts to default interpretation.
19 # The minus sign will be interpreted as a separator.
20 echo $var1 # a+b+c
21 echo $var2 # d e f
22 echo $var3 # g,h,i
23
24 echo
25
26 IFS=","
27 # The comma will be interpreted as a separator.
28 # The minus sign reverts to default interpretation.
29 echo $var1 # a+b+c
30 echo $var2 # d-e-f
31 echo $var3 # g h i
32
33 echo
34
35 IFS=" "
36 # The space character will be interpreted as a separator.
37 # The comma reverts to default interpretation.
38 echo $var1 # a+b+c
39 echo $var2 # d-e-f
40 echo $var3 # g,h,i
41
42 # ======================================================== #
43
44 # However ...
45 # $IFS treats whitespace differently than other characters.
46
47 output_args_one_per_line()
48 {
49 for arg
50 do
51 echo "[$arg]"
52 done # ^ ^ Embed within brackets, for your viewing pleasure.
53 }
54
55 echo; echo "IFS=\" \""
56 echo "-------"
57
58 IFS=" "
59 var=" a b c "
60 # ^ ^^ ^^^
61 output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
62 # [a]
63 # [b]
64 # [c]
65
66
67 echo; echo "IFS=:"
68 echo "-----"
69
70 IFS=:
71 var=":a::b:c:::" # Same pattern as above,
72 # ^ ^^ ^^^ #+ but substituting ":" for " " ...
73 output_args_one_per_line $var
74 # []
75 # [a]
76 # []
77 # [b]
78 # [c]
79 # []
80 # []
81
82 # Note "empty" brackets.
83 # The same thing happens with the "FS" field separator in awk.
84
85
86 echo
87
88 exit