The echo printf command for Shell programming

Source: Internet
Author: User

Both Echo and printf in the shell can be used as output, and printf is an enhanced version of Echo

Show escape characters

echo \ "Abcdef\" "

>>> "ABCdef"

Show variables

Age=23

echo "My Age is $age"

>>>my name is 23

In the process of use, in order to avoid ambiguity, more use of ${age}

Show line breaks

echo "ok\n"

echo "My name is Liming"

>>>ok

>>>my name is Liming

Show Results Redirection

e Cho "My name is Limng" > file1

printf formats the output statement, but must be followed by a carriage return newline character, unlike Echo does not add

printf "acbdef\n"

>>>abcdef

>>>

# format-string is double quotes

$ printf "%d%s\ n" 1 "abc"

1 ABC

# single quotes are the same as double quotes

$ printf '%d%s\ n' 1 "abc"

1 ABC

# No quotation marks can also be output

$ printf %s abcdef

ABCdef

# The format specifies only one parameter, but the extra parameter is still output in that format, format-string is reused

$ printf %s ABC def

ABCdef

$ printf "%s\ n" abc def

Abc

Def

$ printf "%s%s%s\ n" A b c D e F g h i j

A b C

D E F

G h I

J# If there is no arguments, then%s is replaced with NULL,%d replaces with 0

$ printf "%s and%d \ n"

and 0

# If the string is displayed in%d format, then there will be a warning indicating an invalid number, at which point the default is 0

$ printf "The first program always prints '%s,%d\ n'" Hello Shell-bash: printf: Shell: Invalid Numbe

The first program is always prints ' hello,0 '

$

The echo printf command for Shell programming

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.