Print output from the Linux shell

Source: Internet
Author: User

Introduction

Often need to deal with shell commands, but there has been no systematic learning, then it will take 1-2 months to learn the shell command system, then open the magical journey of the shell command. This chapter focuses on the print output of the shell.

Key points of knowledge

    • Shell scripts start with #!/bin/bash; this is a fixed notation, where/bin/bash is the path to the Bash command.
    • The executable permissions for shell scripts are generally granted through chmod.
    • Print output in shell scripts usually has echo and printf two, the former will be wrapped automatically.
    • In the shell, if you use double quotation marks ("") as the content of the printout, if there are special characters in the double quotation marks that need to precede the special characters with the escape character \, you can also use single quotation marks or do not use quotation marks to output directly, use single quotation marks or do not use the escape character.

Format alternates

Some commonly used format alternates are often used in printouts, and format alternates can only be used in printf output .

%-5s: Represents a 5-character width with a-left alignment

%-4.2F: where F is a floating-point type, 4.2 is 4 characters long and the decimal point is 2 bits, and if the decimal point is more than 2 rounded,-left justified.

Plastic:

%d: integer parameter will be converted to a signed decimal number

%u: integer parameter will be converted to an unsigned decimal number

%o: integer parameters are converted to an unsigned octal number

%x: An integer parameter is converted to an unsigned hexadecimal number, denoted by a lowercase abcdef

%x: An integer parameter is converted to an unsigned hexadecimal number, denoted by an uppercase abcdef


floating-point type number :

The parameters of the%f double are converted to decimal digits, and the default is six digits below the decimal point, rounded

The parameters of the%e double type are printed in exponential form, with a number that precedes the decimal point, six digits after the decimal point, and in the exponent section in lowercase e.

%E and%E function the same, the only difference is that the exponent portion will be in uppercase E.

The parameters of the%g double type are automatically selected to be printed in%f or%e format, which is determined by the values to be printed and the number of significant digits that are set.

%G is the same as%G, and the only difference is that the%e format is selected when printing in exponential form.


Characters and Strings:

%c reads the first character of a string

%s outputs character content of the specified width.

%p If the parameter is a "void *" type pointer, it is displayed in hexadecimal format

Print output

When you write a script, you use echo as the printout, and you habitually use double quotes.

Echo

[[email protected] tmp] # echo "Hello word" Hello word

Printf

Default printf is non-wrapped

[[email protected] tmp] # printf "Hello word" Hello word [[email protected] tmp]

Need line break plus \ n parameter

[[email protected] tmp] # printf "Hello word\n" Hello word [[email protected] tmp]

Writing shell scripts

Vim scrip.sh

 #! / bin/  bashprintf " %- 5s %- 10s %- 4s\n "No Name mark;printf"  %- 5s %- 10s 4 . 2f\n "1  aaa 10.111  ;p rintf " %- 5s %- 10s %- 4 . 2f\n "2  BBB 20.146 ; 

Grant Script execution Permission: chmod u+x scrip.sh

If you use echo, you cannot use the format substitute

#! /bin/110.111220.146;

Precautions

When you use the-e,-n parameter in echo,printf,-e,-n should appear before other characters in the command line.

-E: Use the-e parameter if you want to use escape sequences as arguments in Echo's double quotes

-N: Ignore end of line break

Do not use-e parameter [[email protected] tmp]# echo "1\n2"1 \n2
Use -e parameter [[email protected] tmp]-E "1\ N2 "12

Escape sequence:

/N: Line break

/t:tab Key

Color output

Font colors include: 0 = reset, 30 = black, 31 = red, 32 = green, 33 = yellow, 34= blue, 35 = Magenta, 36 = cyan, 37 = White

Background colors include: 0 = reset, 40 = black, 41 = red, 42 = green, 43 = yellow, 44= blue, 45 = Magenta, 46 = cyan, 47 = White

-E "\e[1;32m Hello word \e[0m"

\E[1;32M: Set the font color to green, \e[0m: Resets the color

Summary

The shell commands are very powerful and will continue to post articles on Shell learning, and you are welcome to study together.

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

Print output from the Linux shell

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.