One, the read command reads the file automatically removes the space before and after the line.
A.txt: (There are spaces before and after each line)
11111
222222
3333333
A.sh:
#!/bin/bash
While Read Oneline;do
echo "$oneline"
Done <./a.txt
Execution Result:
111111
222222
333333
Second, use the ${variable name when using a variable in a string}
A.sh:
#!/bin/bash
A= "1"
b= "2"
echo "$a _$b"
Execution Result:
2
Modify the following:
echo "${a}_${b}"
Execution Result:
1_2
Reason:
In the first script, the program goes to get the value of A_ because there is no definition, so it is empty.
Three, the echo when the strange space
A.sh:
#! /bin/bash
Echo-ne \
"123\n" \
"123\n" \
"123\n"
Execution Result:
123
123
123
The second and three lines have more than one space, the reason is unclear, but you can use the following method to avoid.
1.
Echo-ne \
"123\n" \
"123\n" \
"123\n"
2.
Echo-ne \
"123" \
"\n123" \
"\n123\n"
Four. The Select menu item contains spaces
A.sh:
#! /bin/bash
A= "\" 123\ "\" quit without saving\ "\" 32131\ ""
Select B in $a
Do
:
Done
Operation Result:
1) "123"
2) "Quit
3) without
4) Saving "
5) "32131"
#?
Avoidance method:
#! /bin/bash
A= ("123" "Quit Without Saving" "32131")
Select B in "${a[@]}"
Do
:
Done
Operation Result:
1) 123
2) Quit without saving
3) 32131
#?
Five, read after the disappearance of the backslash
A.txt:
123
32\1
1/23
a.sh:
#!/bin/bash
While read Oneline;do
& nbsp; echo $oneline
Done < a.txt
Execution Result:
123
321
1/23
where \ is treated as a newline character of the command, It's automatically removed.
Remediation Method:
a.sh:
#!/bin/bash
while read-r oneline;do
Echo $oneline
Done < a.txt
Execution Result:
123
321
1/23
-R option: Specifies that the read command handles a \ (backslash) as part of the input line, Instead of taking it as a control character.