Tlcl-the world echo from the Shell's eyes

Source: Internet
Author: User
Tags arithmetic echo d

See the world from the shell 's eyes

echo- display a line of text


When you enter a command,bash expands on the characters you enter before executing the command.


Path expansion

echo *

The Shell expands * to the name of the file under the current directory and prints out the name of the file in the current directory .

echo d* print out all files starting with D in the current directory

echo *d prints out all files in the current directory ending in D

~ Expand

echo ~ Print out the current user's home directory information

echo ~test Prints The home directory information of the test user

Arithmetic expression expansion

echo $ ((expression))

echo $ (((2*3) +5))

echo $ (((2**3))

Note the format of the expression, and the arithmetic expression supports only integers and subtraction, take-rest, and power operations.

Curly braces unfold

Create multiple text strings from a pattern of curly braces

Echo hello{1,2,3} will output hello1 Hello2 Hello3

echo Hello {A-i} will output Hello 1 2 3

Echo Hello{a,b,c} will output Helloa hellob helloc

Note: the elements in the {} are to be separated, can be characters, or they can be strings

The most common application is to create a series of files or directories

mkdir hello{1,2,3,4,5}

Parameter expansion

echo $USER print out user name

View the list of variables

Printenv |less

When the expanded variable does not exist, the system expands it into an empty string

Echo $HELLO

Command substitution

To use the output of a command as an expanded

echo $ (LS)

Ls-l $ (which CP) takes the output information of the whic CP as a parameter of ls

Or

Ls-l ' which CP ' legacy Shell support, inverted quotes

Reference

echo Hello World

Will print the Hello world on the screen

echo the total is $100.00

The total is 00.00 will be printed on the screen.

In the first one, theshell removes the extra space, and in the second,it expands the variable to 1 , and 1 is not a variable, so it is expanded into an empty string

The shell provides a mechanism for referencing, which effectively controls the expansion of characters

Double quotes

The first type of reference to control the expansion of characters

If you add "" at both ends of the text,other characters will not have a special expansion meaning except for $ \ '(inverted quotation marks)

In double quotes, parameter expansion, expression expansion, command expansion still valid

echo "$USER $ ((2**3)) $ (LS)"

echo "Hello World" will print out Hello world

Word segmentation mechanism

echo Hello World

This will output Hello World

By default, the word segmentation mechanism looks for spaces, tabs, line breaks in words, and sees them as a qualifier between words, and each word is a parameter, in the example above, there are two parameters, and each parameter is plotted with a space.

Similarly, if you enter echo $ (cal)

At this point, a line of data is printed with a blank space, without a newline character

If you enter echo "Hello World"

Word segmentation is forbidden, in fact, "" internal content as a parameter exists , without adding "" is two parameters

See the difference between echo $ (cal) and echo "$ (cal)"

Single quotation marks

The second type of reference restricts the expansion of characters, and single quotation marks limit the expansion of all characters , all characters have their own meaning, and there is no special meaning

Escape character

Use \ to suppress the expansion of special characters, note that in single quotes \ will lose function

echo \ "Hello world\"

Use of anti-slash escape sequences

\a issued a warning sound;

\b Delete the previous character;

\c finally not add the line break symbol;

\f the line but the cursor remains in its original position;

\ n wraps and the cursor moves to the beginning of the line;

\ r the cursor moves to the beginning of the line, but does not wrap;

\ t insert tab;

\v is the same as \f ;

For example \a bells

The Echo-e ' \a '- e option interprets the escape character , although \ loses its effect in single quotes, but the- e option explains it

echo $ ' \a '

Can not directly echo "\a"

Finally, learn a- n option

-N non-wrapped output

echo "Hello"; echo "World" will now output Hello world in two lines

Echo-n "Hello"; echo "World" will be output in one line HelloWorld

Tlcl-the world echo from the Shell's eyes

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.