[Linux] what is ifs? linuxifs
1. Introduction of IFS
IFS (Internal Field Seprator), which is The Internal domain separator. the complete definition is "the shell uses The value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when implements Ming variable substituioin. ".
What is the use of IFS? The default value of IFS is space, tab, and line break:
Questions about IFS in Shell
IFS variables in shell
IFS and parameter input involve a lot. To give a simple example, we need to enclose the value in quotation marks if there is space when assigning values to variables. Understanding IFS is helpful for shell programming.
Ii. IFS and TMD hit a ghost
Today, when I sorted out the Linux environment variables, I found that there was a big deal, and IFS hit the ghost. I have no answer since I verified it in the morning.
1. Where is BACKGROUND a. IFS?
After logging on to the system, find the image of IFS. Find the global environment variable in the logon shell, env | grep IFS, not in; find the local environment variable in the logon shell, set | grep IFS, which exists. That is to say,IFS exists in local environment variables of shell logon..
[work@localhost ~]$ env |grep IFS[work@localhost ~]$ set |grep IFSIFS=$' \t\n'
B. Where is IFS used?
According to the content verified in the previous blog "fully interpreting Linux environment variables", the scope of local variables is only in the current shell process environment and cannot be used in the parent shell environment, it cannot be used in a sub-shell environment. As a local environment variable for logging on to the shell, IFS is only available in the logon shell environment. Here, verify again. In the login shell, replace the various set local variables testing = "zhangsan", and then access this local variable in the script. If the access fails, it is proved that the local variables are not owned by the fork sub-shell environment.
[work@localhost local]$ testing="zhangsan"[work@localhost local]$ cat run.sh #!/bin/bashecho $testing[work@localhost local]$ sh run.sh [work@localhost local]$
The final execution script does not access the defined local variable testing, which proves
Local Environment Variables cannot be owned by the shell process..
C. No personalization
According to the content verified in the previous blog "fully interpreting Linux environment variables", starting shell, logging on to shell, and interactive shell will pre-load and execute some files at the time of entry, this process can be understood as "Environment initialization" or "personalization". Of course, the non-interactive shell (that is, the execution script) also has such a process, that is, the environment variable BASE_ENV, however, by default, this environment variable is not set and does not exist. That is to say, generally, script execution is not personalized.
2. Questions
If you have already figured out the two prerequisites described in the background, the problem arises.
-------------------------------------------------------------------------------
1. IFS is used as a local environment variable for shell logon;
2. The scope of local environment variables is the current shell process;
3. When executing the script, there is no personalization (or "Environment initialization;
-------------------------------------------------------------------------------
Problem: Why can IFS be used in the script?
Repeat the question again: Why can IFS be used in the script as a local environment variable for shell login?
-------------------------------------------------------------------------------
In fact, you can indeed use it in IFS, And the IFS value is the same as that in the logon shell. Isn't that a big hit? I cannot understand it. I haven't figured it out after a day. Please help me !!!
3. Answers
In Linux, each process has its own environment (the char * env [] parameter of the main function points to). The environment is composed of a group of variables, these variables contain context information that may be referenced by the process. Bash saves copies of environment variables in the global VAR_CONTEXT structure named shell_variables in variables. c. The variables to be exported to the child process are recorded by the global string pointer char ** export_env, in the form of a "name = value" String Array, that is, the content displayed by typing the export command.
After bash is started, call the initialize_shell_variables () function in variables. c, input the env parameter from the main function, and save the environment variables in env to shell_variables. For the environment variables used by bash such as PATH, IFS, and PS1, if there is no env, it is created at this time. In addition, some environment variables related to bash versions, command history, email check, and other internal auxiliary functions are also created here.
Before executing a command, call the functions of various commands in execute_assist.c to call the maybe_make_export_env () function in variables. c to build the environment for exporting to the sub-process, that is, export_env. Shell_execve () uses the execve () function in the exec family when executing external commands. Therefore, export_env can be passed to the subprocess started by bash.
Call the bind_variable () function in variables. c to add and modify environment variables. For example, you need to reset PWD after executing the cd command.
From: Click to open the link
Thanks To Netease's elder brother LIN Xin for answering his family questions, haha ~
Iii. Clever comparison between IFS and $
If IFS and $ are co-located, it is totally framed.
1. We have two different things.
IFS and $, single quotes, and double quotes are two different things. One is a man and the other is a woman. They just saw a hand and were seen by people who did not know the truth, it is said to have become a "Harmony ".
IFS is often used to separate data in the read command, parameter extension, and command replacement, while $ ''and $" are the Quoting syntax in bash shell. You can use the man bash command to view more details in the Quoting section and extract some content related to this article.
Since the CSND Image Upload is too bad (there are infinite bugs in the background when the new and old versions alternate), the size chart often occurs. I will paste the text directly.
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specifiedby the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) \b backspace \e \E an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) \cx a control-x characterThe expanded result is single-quoted, as if the dollar sign had not been present.A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. Ifthe current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
2. We are together
IFS and $ are often put together in double quotation marks. One reason is that the default value of IFS is defined as IFS = $ '\ t \ n ', another major reason is that when writing a shell script, most people need to modify the IFS value. They will struggle to add the "$" symbol and add it to the usable value. It may also work without adding it, is it adding or not?
Add or not, depending on the needs, because there is a small loss in the middle.Read down.
3. IFS, $, and single quotes
To facilitate data processing, you often need to modify the value in the script. For example, if you do not want to use the default value to separate the values, you need to use commas to separate them. You can define IFS = ', ', you can also define IFS = $', ', and you can define IFS = $ ",". What is the difference between them? First, do not complicate the simple problem. These three statements are all value assignment statements in Linux shell. Translate the statements on the left and assign them to the right, so the right side of the discussion will be more direct (as shown in the following code), but I'm sorry, there is no difference here, they all refer to a simple and common comma.
[work@localhost bin]$ echo ",",[work@localhost bin]$ echo $',',[work@localhost bin]$ echo $",",
Unexpected. The default value of IFS is space, tab, and line break. If you do not want to use so many characters, you only want to use one of the tabs or line breaks to separate them (here we will discuss it with line breaks, because it is easier to observe), you can also use the above three methods to re-assign IFS values, IFS = '\ n', IFS = $ "\ n ", IFS = $ '\ n', which is different. The first is normal character backslash and n, and the second is line break, however, the conversion is only performed during execution. The third is a converted carriage return letter NL, and the carriage return is directly displayed on the screen.
[work@localhost bin]$ echo '\n' \n[work@localhost bin]$ echo $"\n"\n[work@localhost bin]$ echo $'\n'[work@localhost bin]$
Review The man bash definition in The previous article, which includes $, escape characters, and single quotes. "Backslash escape sequences, if present, are decoded as follows: options, The expanded result is single-quoted, as if the dollar sign had not been present. ", backslash translation character. If this character exists in the following options, it will be decoded. Therefore, $ '\ n' is finally decoded into the Linux line feed command.
Bytes ------------------------------------------------------------------------------------------------------------------------------
Pseudo conclusion:
If the IFS value is a common character, adding or not adding $ does not matter, because the performance is consistent;
If the IFS value is a special character defined by the system (or the referenced Code contains examples) above, adding $ will be decoded and translated by the system, and its performance is indeed separated by line breaks; if this parameter is left blank, it is a common character, but it is translated again during the parsing process. Therefore, in the separation process, apart from the data itself, "\ n" is also considered as data, therefore, it will be separated separately. Please refer to the following script debugging information. "\ n" is treated as data and separated, but it is not a separator, due to the translation character, it is parsed into a line break. More interesting verification codes are not listed here.
#!/bin/bashIFS='\n'str="a\nb\nc\nd\\n"echo $strfor i in $strdo echo $idoneecho "------"str2="anbncndn"echo $str2for ii in $str2do echo $iidone
[work@localhost local]$ sh -x run2.sh + IFS='\n'+ str='a\nb\nc\nd\n'+ echo a '' b '' c '' d ''a b c d + for i in '$str'+ echo aa+ for i in '$str'+ echo+ for i in '$str'+ echo bb+ for i in '$str'+ echo+ for i in '$str'+ echo cc+ for i in '$str'+ echo+ for i in '$str'+ echo dd+ for i in '$str'+ echo+ echo ------------+ str2=anbncndn+ echo a b c da b c d+ for ii in '$str2'+ echo aa+ for ii in '$str2'+ echo bb+ for ii in '$str2'+ echo cc+ for ii in '$str2'+ echo dd
Bytes ------------------------------------------------------------------------------------------------------------------------------
Conclusion:
1. The characters in IFS can be blank characters (spaces, tabs, carriage returns) and non-blank characters. If they are blank characters, the blank characters before and after the data are ignored. If they are not blank characters, they are not ignored;
2. If there are multiple consecutive blank characters, it is considered as a separator while multiple consecutive non-blank characters are considered as multiple separators;
3. if the defined IFS value is the same as the special characters reserved by the system and you want it to be decoded into special characters in advance, use $ to modify it. Otherwise, the effect is the same as writing special characters in code. It will be transcoded to system special characters only when used;
Bytes ------------------------------------------------------------------------------------------------------------------------------
4. IFS, $, and double quotation marks
In Linux shell, the performance is the same as that without the $ symbol. I cannot understand it deeply here. sorry ···
5. Just name it
If IFS and $ are together, it's not a coincidence!
Note:
If there are any mistakes or omissions in this article, please correct them. Thank you!
I am not very grateful for any questions raised in section 2 of this article!