The terminal is an interactive tool through which users can interact with the shell environment. Printing text in the terminal is a basic daily task for most shell scripts and tools. Through terminal printing, people can know the running state of the system, which is very important for users.
Echo terminal printing
echo ‘Welcome to Bash’
echo Welcome to Bash
The effect of the above three methods is the same. The output content is “welcome to bash”, and a newline character is added at the end. By default, echo adds a line break after each call without entering a line break manually. Here, it is suggested to form a habit and understand other methods.
Restrictions when using double quotes
When using double quotation marks to output a string, the string cannot contain special characters (!), Or you should add the escape character \ beforethe special character. Special characters can also be output normally with or without single quotation marks.
echo “cannot include \!” # in this case, it can be output normally!
echo cannot include ! # in this case, it can be output normally!
echo ‘cannot include !’ # in this case, it can be output normally!
Restrictions on using single quotes
When a string is output in single quotation marks, variable substitution will fail. Using double quotation marks or no quotation marks can output the replaced variable normally.
Echo ‘1234 $var’ # in this case, 1234 $var will be output
Echo 1234 $var # in this case, 1234abcd will be output
Echo “1234 $var” # in this case, 1234abcd will be output
Restrictions when quotation marks are not used
Semicolons (;) cannot be displayed without using quotation marks to output strings, Because semicolons are used as command delimiters in bash. Use single or double quotation marks to output semicolons.
Printf terminal printing
Printf uses the same parameters as the printf function in C language, using reference text or parameters separated by spaces. In the printf function, we can use the format string to specify the width of the string, left and right alignment, etc. By default, printf does not add line breaks at the end of the line and needs to be added manually.
Printf “% – 5S% – 10s% – 4S \ n” no name mark # specifies the width and left alignment
printf “%-5s %-10s %-4s\n” 1 Sarath 80.3456
%s. % C,% D,% F are format substitution characters, and their corresponding parameters can be placed after the quoted format string.
%-5S indicates a string replacement with a format of left alignment and a width of 5, – indicates left alignment. If no alignment is specified, the right alignment is adopted by default. Width specifies the number of characters reserved for a variable. If the width of the content to be output is insufficient, it will be filled with spaces. If it is more than the specified width, the excess part will be discarded.
%4.2f indicates that two decimal places can be reserved for the specified decimal place. Note that 4 does not represent the number of digits in the integer part, nor the sum of integer and decimal places.
Note: when using the command options of echo and printf, make sure that the option appears before all strings in the command line. Otherwise, bash will treat the option as another string.
Using escape characters in echo
By default, echo automatically adds a line break at the end of the output string. You can use the – N option to ignore the line break at the end. Echo also accepts an escape sequence within a double quoted string as a parameter. If the escape sequence needs to be used, the form of echo – e “string containing escape sequence” is used in Ubuntu 14 04, there is no need to add the – e option. If it is added, the – e option will be output as part of the string.
Echo – n “Hello wordl” # does not add a newline at the end
Echo – e “1 \ T2 \ T3” # will output – E 1 # 2 # 3
Echo “1 \ T2 \ T3” # will output 1 # 2 # 3
Print color output
Generating color output in the terminal helps us quickly locate specific information from a large number of text. We can use escape sequence to realize color output.
Each text color has a corresponding color code.
echo -e “\e[1;41m This is red background \e[0m”
#ubuntu14. 04 does not support changing text or background color in this way