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