Transferred from: Feng yunzhi:Http://witmax.cn/shell-echo-spaces.html
When the echo variable is in shell, only one space is printed in the variable, as shown below:
1 Sh-3.2$ TMP="A B d"
2 Sh-3.2$ Echo $ TMP
3 A B d
The solution is to add double quotation marks to the variable, as shown below:
1H-3.2$ TMP="A B d"
2Sh-3.2$ Echo"$ TMP"
3A B d
As for the reason, you need to understand the shell parsing process:
- Replace variable
- Execute Command
- Divide the parameters after the command by IFS, and use the section enclosed by "" and "as a parameter of the command.
The execution process of ECHO $ TMP is:
- Replace variable: echo a B D
- Run echo a B D.
- Divide parameters A, B, and d after the echo command by IFS
Therefore, the output is a B D, and only one space is displayed.
ObserveCodeAlso known
1 Sh - 3.2 $ TMP = " A B d "
2 Sh - 3.2 $ Echo $ TMP
3 A B d
4 Sh - 3.2 $ Echo a B c
5 A B C
6 Sh - 3.2 $ Echo " $ TMP "
7 A B d
8 Sh - 3.2 $ Echo " A B C "
9 A B C