Linux Shell Script Tutorial series (ii): Detailed instructions for terminal printing

Source: Internet
Author: User
Tags printf

This article mainly introduces the Linux Shell Script series (ii): The terminal printing instructions, this article focuses on the echo terminal printing, printf terminal print two print out output command, the need for friends can refer to the

Terminal printing

A terminal is an interactive tool through which users can interact with the shell environment. Printing text in a terminal is a basic task that most shell scripts and tools need to perform on a daily basis. Through the terminal printing, people can know the running state of the system, which is critical to the user.

echo Terminal printing

The code is as follows:

echo "Welcome to Bash"

Echo ' Welcome to Bash '

Echo Welcome to Bash

The effect of the above three methods is the same, the output is "Welcome to Bash", and a line break is added at the end. By default, Echo adds a newline character after each call without having to enter a newline character manually. Here, it is recommended to develop a habit of understanding other methods.

Restrictions when using double quotes

When you use double quotes to output strings, you cannot have special characters (!) in the string, or you want to add an escape character before a special character. You can also output special characters normally by using single quotes or without quotes.

The code is as follows:

echo "cannot include!" #这种情况下将会报错

echo "cannot include!" #这种情况下可以正常输出!

echo cannot include! #这种情况下可以正常输出!

echo ' cannot include! ' #这种情况下可以正常输出!

Use single quotes to limit

When you use single quotes to output strings, variable substitution is invalidated. Use double quotes or do not use quotation marks to output a variable that is replaced normally.

The code is as follows:

Var= "ABCD"

Echo ' 1234$var ' #这种情况下将输出 1234$var

echo 1234$var #这种情况下将输出 1234ABCD

echo "1234$var" #这种情况下将输出 1234ABCD

Restrictions when not using quotes

You cannot display a semicolon (;), because semicolons are used as command delimiters in bash, without using quotation marks to output strings. Use single quotes or double quotes to output semicolons.

The code is as follows:

Echo Hello;hello #第一个hello被当成字符串输出, and the second was taken as an order.

printf Terminal printing

printf uses the same parameters as the printf function in C, using reference text or space-delimited arguments. In the printf function, we can use the format string to specify the width of the string, the left and right alignment, and so on. By default, printf does not add newline characters at the end of a line and needs to be added manually.

The code is as follows:

printf "Hello World" #使用引用字符

printf "%-5s%-10s%-4sn" No Name Mark #指定宽度及左对齐方式

printf "%-5s%-10s%-4sn" 1 Sarath 80.3456

Working principle

%s,%c,%d,%f are all format substitution characters whose corresponding parameters can be placed after the quoted format string.

%-5s indicates a string substitution with a format that is left-aligned and has a width of 5,-indicates left alignment, and, if no alignment is specified, the default is the right alignment. The width specifies the number of characters to be reserved for a variable, and if the content to be output is insufficient, padding with a space, if more than the specified width, the portion is discarded.

%4.2F indicates that the specified decimal number can retain two decimal places, and note that 4 does not represent the number of digits in the integer part, nor does it represent the number of integers and decimal digits.

Note: When using the command options for Echo and printf, if you want to ensure that the option appears before all the strings in the command line, no bash treats the option as another string.

Supplemental content

Using escape characters in Echo

By default, Echo automatically adds line breaks at the end of the output string, and you can use the-n option to ignore the ending line break. Echo also accepts an escape sequence as an argument within a double quote string. If you need to use an escape sequence, in the form of a echo-e "string with an escape sequence", in ubuntu14.04, you do not need to add the-e option, and if you add, the-e option is exported as part of the string.

The code is as follows:

echo "Hello World" #将在末尾添加换行符

Echo-n "Hello wordl" #末尾不会添加换行符

Echo-e "1t2t3" #将输出-E 1 2 3

echo "1t2t3" #将输出1 2 3

Print color output

Generating color output in a terminal helps us quickly locate specific information from a large number of text, and 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;31m This is red text e[0m"

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

#ubuntu14.04 does not support changing text or background colors in this way

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.