Linux Shell Series Tutorial (vii) Shell output

Source: Internet
Author: User
Tags echo command

This article is part of the Linux Shell Series Tutorial (vii), more shell tutorials See: Linux Shell Series Tutorials

As with other languages, there are output operations in the shell, and in practical applications it is also very important to introduce the shell output operation for you today.

Shell echo Command

The echo command is an internal command of the shell that prints the specified string on the screen.

Command format:
Echo Arg
Escape character

Like other high-level languages, the shell uses the backslash "\" as the escape character.

Example:

echo "\" It is a test\ ""

Output: "It is a test"

Output variables

You can use the echo command to output variables directly, as shown in the following example:

Name= "Linuxdaxue" echo "$name It is a test"

Output: Linuxdaxue It is a test

If the variable is connected to another character, you need to use curly braces ({}), as in the following example:

Mouth=8echo "${mouth}-1-2009"

Output: 8-1-2009

Output line break

The echo command, like other languages, uses the backslash +n ("\ n") to represent line breaks, as shown in the following example:

echo "ok!\n" echo "It is a test"

Output:

Ok!
It is a test

Output redirection

The shell can use the right angle brackets (">") and two right angle brackets (">>") to represent the redirection of the output, which is explained today, and a file is specifically written later in this article to detail the shell's input-output redirection operations.

Example:

echo "It is a test" > myfile# to redirect the string into myfile this file, myfile the content will be erased echo "It is a test" >> myfile# to redirect the string into myfile this file , myfile content is not erased, new content is appended to the end of the file
Remain as-is output

The echo command uses the single quotation mark "'" to keep the output intact, without processing the content. Examples are as follows:

echo ' $name \ '

Output:

$name \ "

Output command Execution results

A command that is enclosed by the Echo post can output the command execution result. ' This symbol is the key of the English half-width keyboard tab and the wavy line, and the command is included in the ' symbol ' to execute the command, which can be used for many complex operations.

Example:

Echo ' Date '

The current time is output

Shell printf Command

The printf command is used to format the output and is an enhanced version of the echo command. It is very similar to the C-language printf, but it is somewhat different in syntax.

Because printf commands are defined by the POSIX standard, portability is better than echo.

printf command syntax
printf  format-string  [arguments ...]

Format-string is the format control string and arguments is the parameter list. It is important to note that printf does not wrap, meaning that using the printf command must explicitly use ' \ n '

The printf command is not introduced today, and you can see the printf command in C, which today focuses on the difference between the shell printf command and the C-language printf command.

    • printf command without parentheses
    • Format-string can be without quotation marks, but it is best to add single quotes and double quotes.
    • When the parameter is more than the format control (%), format-string can be reused and all parameters can be converted.
    • Arguments use spaces separated by commas.
Examples of Use:
# format-string for double quotes $ printf "%d%s\n" 1 "ABC" 1 abc# single quote with double quote effect like $ printf '%d%s\n ' 1 "ABC" 1 abc# no quotation marks can also output $ printf%s ABC  The defabcdef# format specifies only one parameter, but the extra arguments are still output in that format, format-string is reused by the printf%s ABC defabcdef$ printf "%s\n" ABC defabcdef$ printf "%s  %s%s\n "A b c D e F g h i ja b cd e FG H ij# If there is no arguments, then%s is replaced with null,%d with 0 instead of $ printf"%s and%d \ n "and 0# if%d Format to display the string, then there will be a warning indicating the invalid number, at which point the default is 0$ printf "The first program always prints '%s,%d\n '" Hello SHELL-BASH:PRINTF:SHELL:INV Alid numberthe First program always prints ' hello,0 ' $

Well, the relevant knowledge of the shell output is introduced here first, we quickly test it.

For more shell tutorials See: Linux Shell Series Tutorials

This article fixed link: Linux University network _linux learning _shell_ embedded Linux--linux Shell series of Tutorials (vii) Shell output

Linux Shell Series Tutorial (vii) Shell output

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.