Shell Command sorting

Source: Internet
Author: User

1. Echo Parameters

  • -N does not wrap (default line feed)
  • -E makes the transfer character valid

Example

Ss = 'abc \ n' echo-e $ ssabc # echo $ ssabc

2. When assigning values = do not include spaces on both sides

A = "hello" # error A = "hello"

3. Shell operations

b=4let "c=$b+1"echo $c

4. Differences between single quotes and double quotes

Single quotation marks and double quotation marks are the same as string delimiters, rather than character delimiters. The single quotation mark is used to keep the literal value of all characters in the quotation marks, even if the \ and carriage return in the quotation marks are no exception.

Example

 bb="hello"echo "$bb"helloecho ‘$bb‘$bb

Note that the ecoh-e '$ BB' result is still $ BB, because '$ BB' is the literal value and will not be escaped.

5. Array

5.1 declaration and definition

  • Array ['a'] = 1
  • Array = ('A', 'B ')
  • Array = ([1] = 'A' [2] = 'B ')

5.2 access

  • Echo $ {array ['a']}
  • Echo $ {array [0]}

5.3 Traversal

for var in ${array[*]}do    echo $vardone

5.4 array Length

num=${#array[@]}    

Comparison: get String Length: num =$ {# STR}

6. multithreading in Linux Shell

for ((i=0;i<5;i++)); do{    sleep 3;    echo "hello" $i}donewait

Comparison

for ((i=0;i<5;i++)); do{    sleep 3;    echo "hello" $i}&donewait

This adds a background execution & symbol to the above. At this time, five cyclic tasks are executed concurrently, and at last it takes more than 3 seconds (the effect is very obvious ). The purpose of wait is to execute all the tasks in the background, otherwise the program will wait.

7. Remove '\ R' from the last row'

'\ R' at the end of the string is very disgusting. You can use the tr command to remove it.

A = "Hello \ r" Echo $ A | tr-d' \ R' Hello \ recho $ A | tr-d "\ r" Hello \ recho-e $ A | tr -d' \ r'hello # Transfer Character function echo-e $ A | tr-d "\ r" Hello

8. Compile the shell file

bash -x ***.sh

+ Starts with the intermediate compilation process, and the output without + is normal. The effect is very obvious.

 

Shell Command sorting

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.