Linux Shell Script Raiders (1.2)

Source: Internet
Author: User

1.2 Terminal printing
    • A terminal is an interactive tool that allows users to interact with the shell environment. Printing text in a terminal is a basic task that most shell scripts and tools need to perform daily. Through the terminal printing, people can know the operating state of the system, which is critical to the user.
Echo Terminal Print
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 add line breaks at the end. By default, Echo adds a newline character after each call without having to manually enter a line break. Here, it is advisable to develop a habit of understanding other methods.

    • limitations when using double quotation marks
      When you use double quotation marks to output a string, the string cannot contain a special character (!), or you want to put the escape character in front of a special character \. You can also output special characters normally by using single quotation marks or without using quotation marks.

echo"cannot include !"   #这种情况下将会报错echo"cannot include \!"  #这种情况下可以正常输出!echoinclude !     #这种情况下可以正常输出!echo‘cannot include !‘   #这种情况下可以正常输出!
    • limitations of using single quotation marks
      When you use single quotes to output a string, the variable substitution is invalidated. Use double quotes or do not use quotation marks to output the substituted variable normally.
var="abcd"echo‘1234$var‘          #这种情况下将输出 1234$varecho1234$var            #这种情况下将输出 1234abcdecho"1234$var"          #这种情况下将输出 1234abcd
    • limitations When quotation marks are not used
      You cannot display a semicolon (;), because the semicolon is used as a command delimiter in bash, without using quotation marks to output a string. Use single or double quotation marks to output semicolons.
echo hello;hello  #第一个hello被当成字符串输出,第二个被当成命令
printf Terminal printing

printf uses the same parameters as the printf function in C, using reference text or arguments separated by spaces. In the printf function, we can use a formatted string that specifies 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.

printf"Hello world"                    #使用引用字符printf"%-5s %-10s %-4s\n"#指定宽度及左对齐方式printf"%-5s %-10s %-4s\n"180.3456
    • Working principle
      %s,%c,%d,%f are format substitution characters , and their corresponding parameters can be placed after the quoted format string.
      %-5s indicates a string substitution with a left-aligned, width of 5, and a left-aligned, right-aligned default if no alignment is specified. The width specifies the number of characters that are reserved for a variable, and if the content to be output is not wide enough, a space is filled, and if more than the specified width, the portion is discarded.
      %4.2F indicates that a specified decimal can retain two decimal places, noting that 4 does not represent the number of digits of the integer part, nor does it represent the number of integers and decimal places.

    • Note: when using the Echo and printf command options, if you want to make sure that the option appears before all strings in the command line, 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 end of the newline character. Echo also accepts an escape sequence within a double-quoted string as an argument. If you need to use escape sequences, this form of Echo-e "string containing escape sequences", in ubuntu14.04, does not need to add the-e option, and if added, the-e option is output as part of the string.
echo"Hello World"           #将在末尾添加换行符echo"Hello Wordl"        #末尾不会添加换行符echo-e"1\t2\t3"            #将输出 -e 1  2   3echo"1\t2\t3"               #将输出1  2   3
    • Print color output

    • Generating color output in the terminal helps us quickly locate specific information from a large amount of text, and we can use escape sequences to achieve color output.

    • Each text color has a corresponding color code.

Reset
Black Red Green Yellow Blue Magenta Cyan White
0 30 31 32 33 34 35 36 37

-Each background color also has a corresponding color code.

Th align= "center" > Green TD align= "Center" >42
reset black Red yellow Blue Magenta Cyan White
0 40 41 43 44 45 46 47
echo-e"\e[1;31m This is red text \e[0m"echo-e"\e[1;41m This is red background \e[0m"#ubuntu14.04不支持这种方式改变文本或者背景颜色
Reference

Linux Shell Script Raiders

Linux Shell Script Raiders (1.2)

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.