Shell: ECHO does not display multiple spaces in the variable.

Source: Internet
Author: User
Tags echo command

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:

    1. Replace variable
    2. Execute Command
    3. 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:

    1. Replace variable: echo a B D
    2. Run echo a B D.
    3. 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.