While studying shell scripts (bash), the behavior of for statements when IFS variables are changed
I have a question because I have a problem.
GNU bash, version 4.4.19 (3) -release (i686-pc-msys)
GNU bash, version 4.2.46 (2) -release (x86_64-redhat-linux-gnu)
(1) First, the following shell was executed with $IFS set to default (blank, line feed, horizontal tab).
for i in abc def do echo string = $i done
string = abc string = def
(2) Next, I changed $IFS to "quote" and executed a similar shell.
IFS = / for i in abc // def do echo string = $i done
string = abc def
- As for the execution result of (2), it was assumed that it will be output with a delimiter as shown in (1).
However, the line feed is not actually output because it is a new line, and $i seems to contain the input value as it is (abc def). Why is this?
- (2), how can I make a line break with a delimiter like the execution result of (1)?
Answer # 1
Man bash makes it difficult to interpret the sentence, but words are re-separated (once separated by whitespace), brace expansion, tilde expansion, parameter and variable expansion, command substitution It seems only after arithmetic expression expansion.
However, in practice, there is no word re-breaking in the results of brace expansion or tilde expansion, and it seems that re-breaking in IFS occurs only in the results of parameter and variable expansion, command substitution, and arithmetic expression expansion. . And documented.
If you want to separate IFS, you need to put it in a variable.
IFS = / FOO = abc // def for i in $FOO do echo string = $i done
- i want to surely get the execution line of the shell (bash) into a variable
- i want to compare a variable with if in bash and echo if it is not either value
- bash - assign the result of wget to a variable
- bash - shell is not changed
- bash - how to assign a global variable in the local scope of a shell script
- bash - i want to call a variable by handling it in a variable
- bash - the contents of the date command stored in the variable are not updated
- bash - questions about nested variables
- linux - i want to specify an option of "more than one character" in getopts of the shell script
- linux - how to make sure all optional parameters are not empty in shell script
- linux - using a shell script to extract and operate multiple specific extension files
- linux - i want to put the contents of all tags between tags in sed
- linux - i want to extract only part of the file name with bash
- bash - i want to output multiple server names to the log
- linux - for bash, if a diff command is interposed in a for loop, the return value of the difference is returned and the loop exi
- linux - i want to check empty characters of multiple optional parameters in shell script
- linux - i want to create three files with different extensions with the same time, minute, and second in a shell script