Linux Shell script series tutorial (2): terminal printing command details

Source: Internet
Author: User

Linux Shell script series tutorial (2): terminal printing command details

This article mainly introduces the Linux Shell script series (2): terminal printing command details, this article focuses on echo terminal printing, printf terminal printing two Print Output commands, for more information, see

Terminal Printing

A terminal is an interactive tool that allows you to interact with the shell environment. Printing text on a terminal is a basic task that most shell scripts and tools need to perform on a daily basis. Through Terminal printing, people can know the running status of the system, which is vital to users.

Echo terminal Printing

The Code is as follows:

Echo "Welcome to Bash"

Echo 'Welcome to bash'

Echo Welcome to Bash

The effects of the above three methods are the same. The output content is "Welcome to Bash" and a line break is added at the end. By default, echo adds a line break after each call, instead of entering a line break manually. We recommend that you develop a habit of understanding other methods.

Double quotation marks

When double quotation marks are used to output a string, the string cannot contain special characters (!), Or add the Escape Character \ before a special character \. Special characters can also be output normally if single or no quotation marks are used.

The Code is as follows:

Echo "cannot include! "# In this case, an error will be reported.

Echo "cannot include \! "# Normal output in this case!

Echo cannot include! # In this case, the output is normal!

Echo 'cannot include! '# Normal output in this case!

Restrictions on the use of single quotes

When a single quotation mark is used to output a string, the replacement of the variable will become invalid. Variables that are replaced can be normally Output Using Double quotation marks or without quotation marks.

The Code is as follows:

Var = "abcd"

Echo '1970 $ var '# in this case, 1234 $ var will be output.

Echo 1234 $ var # in this case, 1234 abcd will be output.

Echo "1234 $ var" # in this case, 1234 abcd is output.

Restrictions when no quotation marks are used

The semicolon (;) cannot be displayed without the quotation mark output string, because the semicolon is used as the command Separator in Bash. You can use single or double quotation marks to output a semicolon.

The Code is as follows:

Echo hello; hello # The first hello is treated as a string output, and the second is treated as a command.

Printf terminal Printing

The parameters used by printf are the same as those used by the printf function in C language. parameters are referenced in text or separated by spaces. In the printf function, we can use a formatted string to specify the width of the string and the left-right alignment. By default, printf does not add line breaks at the end of a row. You need to manually add line breaks.

The Code is as follows:

Printf "Hello world" # Use reference characters

Printf "%-5 s %-10 s %-4s \ n" No Name Mark # specify the width and left alignment

Printf "%-5 s %-10 s %-4s \ n" 1 Sarath 80.3456

Working Principle

% S, % c, % d, % f are all format replacement characters, and their parameters can be placed after the format string with quotation marks.

%-5s indicates that a string in the left-aligned format and with a width of 5 is replaced.-indicates the left-aligned string. If no alignment is specified, the right-aligned string is used by default. The width specifies the number of characters that are reserved for a variable. If the width of the content to be output is insufficient, enter space. If the width is greater than the specified width, the excess part is discarded.

% 4.2f indicates that the specified decimal place can retain two decimal places. Note that 4 does not represent the digits of the integer part, nor the sum of the integer and decimal places.

Note: when using the echo and printf Command Options, make sure that the options appear before all strings in the command line. Otherwise, Bash regards the options as another string.

Supplementary content

Use escape characters in echo

By default, echo automatically adds a line break to the end of the output string. You can use the-n option to ignore the line break at the end. Echo also accepts escape sequences in double quotation marks as parameters. If an escape sequence is required, the echo-e string containing the escape sequence is used. In ubuntu14.04, the-e option is not required, the-e option is output as part of the string.

The Code is as follows:

Echo "Hello World" # adds a line break to the end

Echo-n "Hello Wordl" # No line break is added at the end

Echo-e "1 \ t2 \ t3" # output-e 1 2 3

Echo "1 \ t2 \ t3" # output 1 2 3

Print color output

Generating color output in the terminal helps us quickly locate specific information from a large number of texts. We can use escape sequences to achieve color output.

Each text color has a corresponding color code.

The Code is as follows:

Echo-e "\ e [1; 31 m This is red text \ e [0 m"

Echo-e "\ e [1; 41 m This is red background \ e [0 m"

# Ubuntu14.04 the text or background color cannot be changed in this way

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.