BRIEF DESCRIPTION OF IFS

Many commands in bash will be split words, most of the time the default is to use the space as a separator, sometimes encounter tabs, line breaks will be separated. The most typical is "for i in ab c", it will split the list of variables "ab c" to become three variables. This separator is designated by the variable IFS.

IFS is the internal field separator bash environment variables.

[root@localhost ~]# set | grep IFS
IFS=$' \t\n'

The default IFS encountered in space, tab \ t and breaks \ n separated automatically to the next step. But a little bit different for space processing, and end of the line on both sides of the line does not handle spaces, and multiple consecutive spaces as a single space by default.

Sometimes when writing a script or execution of the loop, IFS modification may play a significant role. If you want to modify the IFS, best to remember to back up the system IFS, where longer needed then restore IFS.

E.g:

[root@localhost ~]# data="name,sex,rollno,location"
[root@localhost ~]# oldIFS=$IFS
[root@localhost ~]# IFS=$','
[root@localhost ~]# for item in $data;do echo Item:$item;done
Item:name
Item:sex
Item:rollno
Item:location
[root@localhost ~]# IFS=$oldIFS

It can be seen that the above example the default setting for separator after the comma, can not easily deal with data variables of the divided fields.

One more interesting example: each character-by-character printing.

[root@localhost ~]# cat /etc/resolv.conf | (IFS=$'\034';while read -N 1 x;do /usr/bin/printf "%s" "$x";sleep 0.1; done)
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver fe80::1%ens33

The above while read -N 1 x represents each character is read and stored into a variable x, as may be read spaces or line breaks, so that the output of printf not so modifying the value of IFS, which is set to "\ 034" , which is a control character, can not appear in the file, so the file in any of the characters are safe.

[root@localhost ~]# cat /etc/passwd |(IFS=$'\034'; while read -N 1 x;do /usr/bin/printf "%s" "$x";sleep 0.1; done)

Most of the time, we are not going to modify the IFS IFS does not expect to achieve by modifying some purpose, but other methods implemented instead. This requires attention to the default IFS ( "\ t \ n") of a special nature, it ignores leading blanks and suffix blank, blank and continuous compression. At some point, it will be unexpected problems arise.

E.g:

[root@localhost ~]# a=-s" "
[root@localhost ~]# echo "$a" | wc -m
4

$ A fact here is three characters, the last character is a space. If the variable is not quoted, then problems will arise in many cases:

[the root @ localhost ~] = A # -s "  " 
[the root @ localhost ~] #   echo $ A | WC - m 
 . 3 
# s later spaces are ignored 
[the root @ localhost ~] # echo  " $ {A: 2 } " | WC - m
 2 
 # taken to the back of the s-box 
[the root @ localhost ~] # echo $ {a: 2 } | WC - m 
 . 1 
 # is not taken into the space behind the s 
[the root @ localhost ~] # expr substr A $ . 3  100 | WC - m
 . 1
# S is not taken to the back of the box 
[the root @ localhost ~] # expr substr " $ A "  . 3  100 | WC - m
 2 
# s taken to the back of the space

Therefore, in the case of a variable can be quoted, although 100 may be in quotes to protect the whitespace characters.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liujunjun/p/12003894.html