1.2.1 The quotation mark problem of echo
Regarding the usage of echo, note that the special case of single and double quotes. Take "Hello World!" As an example.
[[email protected] tmp] # echo Hello World!
Hello World!
[[email protected] tmp] # echo ‘Hello World!’
Hello World!
[[email protected] tmp] # echo "Hello World!" #Double quotes cannot print exclamation mark
-bash:! ": event not found
[[email protected] tmp] # echo Hello World !; echo ‘Hello World!’ # No semicolon after the exclamation mark
-bash:!: event not found
[[email protected] tmp] # echo ‘Hello World!’; echo Hello World! #Exclamation mark can be at the end
Hello World!
Hello World!
From the above experiments, it is found that any symbols and characters except single quotation marks cannot be followed by the exclamation mark, because the setting of using the exclamation mark to refer to the history command in memory is turned on by default, you can use set + H to turn off the setting, then you can use Exclamation mark output.
[[email protected] tmp] # set + H
[[email protected] tmp] # echo "Hello World!"
Hello World!
If echo does not add any quotation marks, it is obviously not possible to output a semicolon ";", only single quotation marks can not be used to expand variables, and the use of double quotation marks is not easy to output exclamation marks, so consider how to use echo.
[[email protected] tmp] # echo Hello World;
Hello World #The semicolon is ignored as a line break symbol
[[email protected] tmp] # str = World!
[[email protected] tmp] # echo ‘Hello $ {str}’
Hello $ {str} #variables are output as ordinary characters
1.2.2 Escape in echo
echo -e recognizes escaped and special meaning symbols, such as newline character / n, tab character / t, escape character \, etc.
[[email protected] tmp] # echo ‘Hello World! \ n’; echo "Hello World"!
Hello World! \ N
Hello World!
[[email protected] tmp] # echo -e ‘Hello World! \ n’; echo "Hello World"!
Hello World!
#Newline
Hello World!
1.2.3 Echo default branch processing
By default, without -n, echo will add a newline symbol at the end of each line, and use echo -n to cancel branch output.
[[email protected] tmp] # echo ‘Hello World!’> abc.sh #Add branch symbol after input
[[email protected] tmp] # echo ‘Hello World!’ >> abc.sh
[[email protected] tmp] # cat abc.sh
Hello World!
Hello World!
[[email protected] tmp] # echo -n ‘Hello World!’> abc.sh # cancelled the branch symbol
[[email protected] tmp] # echo ‘Hello World!’ >> abc.sh
[[email protected] tmp] # cat abc.sh
Hello World! Hello World!
1.2.4 Echo color output
Echo can control the font color and background color output.
Common font colors: reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, purple = 35, sky blue = 36, white = 37.
Common background colors: reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, purple = 45, sky blue = 46, white = 47.
Because special symbols need to be used, they need to be recognized with the -e option.
[[email protected] tmp] # echo -e "\ e [1; 41m Red Bcakground \ e [0m"
[[email protected] tmp] # echo -e "\ e [1; 31m Red Bcakground \ e [0m"
Any one of \ e can be replaced with \ 033. Such as:
[[email protected] tmp] # echo -e "\ 033 [1; 41m Red Bcakground \ 033 [0m"
[[email protected] tmp] # echo -e "\ e [1; 31m Red Bcakground \ 033 [0m"
The color also has some controls: 1 for highlight, 4 for underline, and 5 for flicker.
1.2.5 printf
Use printf to output more regular and formatted results. It is quoted from the C language printf command, but there are some differences.
Use printf to specify the width of the string, achieve left alignment (using minus sign-), right alignment (default), format decimal output, etc. The most important thing to note when using printf is that printf does not add a newline at the end by default. It is not like echo, so you must manually add a "/ n" newline.
[[email protected] tmp] # cat> abc.sh << eof #Overwrite the following content into the abc.sh script
> #! / bin / bash
> #File name: abc.sh
> printf "% -5s% -10s% -4s \ n" No Name Mark #Three% correspond to the following three parameters
> printf "% -5s% -10s% -4.2f \ n" 1 Sarath 80.34 #Minus sign "-" means left aligned
> printf "% -5s% -10s% -4.2f \ n" 2 James 90.998 # 5s means the first parameter takes 5 characters
> printf "% -5s% -10s% -4.2f \ n" 3 Jeff 77.564
> eof
[[email protected] tmp] # sh abc.sh #Execution result: left aligned, take two decimal places
No Name Mark
1 Sarath 80.34
2 James 91.00
3 Jeff 77.56
[[email protected] tmp] # sed -i s # ‘-‘ ## g abc.sh #Remove the minus sign “-”, the result will be right aligned
[[email protected] tmp] # sh abc.sh
No Name Mark
1 Sarath 80.34
2 James 91.00
3 Jeff 77.56
You can also add line breaks, tabs and other symbols to printf.
[[email protected] tmp] # vim abc.sh #Modify abc.sh and change it to the following format
#! / bin / bash
#File name: abc.sh
printf "% -s \ t% -s \ t% s \ n" No Name Mark
printf "% -s \ t% -s \ t% 4.2f \ n" 1 Sarath 80.34
printf "% -s \ t% -s \ t% 4.2f \ n" 2 James 90.998
printf "% -s \ t% -s \ t% 4.2f \ n" 3 Jeff 77.564
[[email protected] tmp] # sh abc.sh #A tab appears
No Name Mark
1 Sarath 80.34
2 James 91.00
3 Jeff 77.56
There is also a common i format for printf, which indicates that formatting an integer takes a few integers, and s in the previous example indicates formatting a character.
SHELL script guide (study notes) --1.2 echo and printf printout