echo and printf print output

[root@node2 scprits]#  echo Hello World!
Hello World!
[root@node2 scprits]# echo 'Hello World!'
Hello World!
[root@node2 scprits]# echo "Hello World!" 
-bash: !": event not found
#双引号不能打印感叹号
[root@node2 scprits]# echo Hello World!;echo 'Hello World!'  
-bash: !: event not found
[root@node2 scprits]# echo 'Hello World!';echo Hello World!
Hello World! 
The Hello World ! 
# Exclamation mark at the end of the most

Several experiments found from above, in bash environment, to use single quotes for output exclamation mark. This is because the use of the exclamation point to open the history command reference memory settings by default, you can use set + H turn off the setting, then you can use the exclamation point output.

[root@node2 scprits]# set +H
[root@node2 scprits]# echo "Hello World!"
Hello World!

Or the use of multiple echo parameters are surrounded by quotation marks, where an exclamation mark, in single quotes, as follows.

[root@node2 scprits]# echo "Don't use rm -rf command"'!'
Don't use rm -rf command!

If the echo without any quotation marks, it obviously can not output the semicolon ";", because the semicolon will be parsed as command connection symbol shell.

[root@node2 scprits]# echo Hello World;
Hello World
[root@node2 scprits]# echo "Hello World;"
Hello World;

But only in single quotation marks can not expand variables, use double quotation marks and bad output exclamation point, a method so the echo command to overcome incurable diseases is a reference to a separate special symbols.

[root@node2 scprits]# str=hello
[root@node2 scprits]# echo "$str"'!' "world"
hello! world

The escape echo              

echo -e recognizes escape symbols and special significance, such as line breaks, n, tab \ T, escape \ and the like.

[root@node2 scprits]# echo 'Hello World!\n';echo "Hello World"!  
Hello World!\n
Hello World!
[root@node2 scprits]# echo -e 'Hello World!\n';echo "Hello World"!
Hello World!

Hello World!

echo default processing branch              

By default, without the echo -n end of each row will add line break, using echo cancellation branch -n output.

[root@node2 scprits]# echo -n 'Hello World!' >abc.sh 
[root@node2 scprits]# echo 'Hello World!' >>abc.sh
[root@node2 scprits]# cat abc.sh
Hello World!Hello World!
[root@node2 scprits]# echo 'Hello World!'>abc.sh
[root@node2 scprits]# echo 'Hello World!'>>abc.sh 
[root@node2 scprits]# cat abc.sh 
Hello World!
Hello World!

echo color output                

echo can control the font color and background color output.

Common font color: Reset = 0, 30 = black, red = 31, green = 32, 33 = yellow, blue = 34, 35 = purple, blue = 36, 37 = white.

Common background color: Reset = 0, 40 = black, red = 41, green = 42, 43 = yellow, blue = 44, 45 = purple, blue = 46, 47 = white.

Font control options: 1 represents a highlight, 4 underlining, and 5 represents flicker.

Because of the need to use special symbols, so the need to meet the -e option to recognize special symbols.

[root@node2 scprits]# echo -e "\e[1;41m Red Bcakground \e[0m"
 Red Bcakground 
[root@node2 scprits]#  echo -e "\e[1;31m Red Bcakground \e[0m" 
 Red Bcakground 

Font color control and define the order control options does not matter, as long as it is defined, shell can identify. Recommended that the definition of the color after color also defines closed, otherwise the color will continue to affect the color of bash environment.

Further, any one of \ e can use \ 033 replacement. Such as:

[root@node2 scprits]# echo -e "\033[1;41m Red Bcakground \033[0m"
 Red Bcakground 
[root@node2 scprits]#  echo -e "\e[1;31m Red Bcakground \033[0m" 
 Red Bcakground 

 printf                

Use printf output can be more regular and more formatted. It refers to the C language printf command, but slightly different.

Printf can be specified using the width of the string, to achieve left alignment (using the minus sign -), right-aligned (the default), and the like fractional output format.

Use printf is most important to note two points:

(1) printf default is not the end of a line feed, it is not the same echo, so to manually add "\ n" number of transducers;

(2) printf only format output, any results will not change, so the output of the floating-point format, the result is the same float, just change the results displayed.

[root @ node2 scprits] # CAT .> abc SH << EOF
 > # / bin /! bash
 > # file name:. abc SH 
> printf " % -5S% -10s% -4S \ the n- " No Mark the Name three # %, a corresponding three parameters behind
 > the printf " % -5S% -10S -4.2f% \ n- '  . 1 Sarath 80.34 # minus" - "indicates left
 > the printf " % -5S% -10S% -4.2f \ the n- "  2 James 90.998 # 5S denotes a parameter occupies five characters
 > printf " % -5S% -10s% -4.2f \ the n- "  3 Jeff 77.564 
> eof
[root@node2 scprits]# bash abc.sh 
No    Name       Mark
1     Sarath     80.34
2     James      91.00
3     Jeff       77.56
[root @ node2 scprits] # sed the -is # ' - ' . ## G abc SH 
[root @ node2 scprits] # CAT . abc SH  
# ! / bin / bash 
. # File name: abc SH 
printf " % 10s% 5S 4S% \ n- , " No Mark # the Name three percent respectively corresponding to the three parameters behind 
the printf " % 4.2f% 5S% 10s \ n- '  . 1 Sarath 80.34 # minus "" left represents 
the printf " % 10s 5S% 4.2% f \ the n- "  2 James 90.998 # 5S denotes a parameter occupies five characters 
printf " % 5S% 10s% 4.2f \ the n- " 3 Jeff 77.564
[root@node2 scprits]# bash abc.sh 
   No       Name Mark
    1     Sarath 80.34
    2      James 91.00
    3       Jeff 77.56
[root @ node2 scprits] # CAT abc. SH  
# / bin /! bash 
# filename:. abc SH 
printf " % 5S \ t% 10s \ t% 4S \ the n- " No the Name Mark # three percent respectively behind three parameters 
the printf " % 5S \ T% 10s \ T% 4.2f \ n- '  . 1 Sarath 80.34 # minus" "left represents 
the printf " % 5S \ T% 10s \ T% 4.2f \ n- "  2 James 90.998 # 5s represents a parameter occupies five characters 
printf " % 5s \ t% 10s \ t% 4.2f \ the n- "  3 Jeff 77.564 
[root @ node2 scprits] # bash abc. SH 
   No          Name      Mark
    1        Sarath      80.34
    2         James      91.00
    3          Jeff      77.56

printf i and a common format for integer format occupancy represents an integer number, represented in the previous example s character format.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liujunjun/p/11979718.html