Shell-related note points

Source: Internet
Author: User
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.

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.