Linux Shell Script Tutorial series (ii): TERMINAL Print command detailed _linux shell
Source: Internet
Author: User
Terminal printing
The terminal is an interactive tool through which users can interact with the shell environment. Printing text in the terminal is a basic task that most shell scripts and tools need to perform daily. Through terminal printing, people can know the running status of the system, which is crucial for users.
echo terminal print
The code is as follows:
echo "Welcome to Bash"
echo 'Welcome to Bash'
echo Welcome to Bash
The effect of the above three methods is the same, the output is "Welcome to Bash", and a newline is added at the end. By default, echo adds a newline after each call, without having to manually enter the newline. Here, it is recommended to develop a habit, to understand other methods.
Restrictions when using double quotes
When using double quotes to output a string, the string must not contain special characters (!), Or you must add the escape character \ before the special characters. With single quotes or without quotes, special characters can also be output normally.
The code is as follows:
echo "cannot include!" #In this case, an error will be reported
echo "cannot include \!" #Can output normally in this case!
echo cannot include! #Can output normally in this case!
echo 'cannot include!' # In this case, it can be output normally!
Restrictions on using single quotes
When using single quotes to output a string, variable substitution will be invalid. Use double quotes or no quotes to output the replaced variables normally.
The code is as follows:
var = "abcd"
echo '1234 $ var' # 1234 $ var will be output in this case
echo 1234 $ var #In this case 1234abcd will be output
echo "1234 $ var" # 1234abcd will be output in this case
Restrictions when not using quotation marks
Without using quotes to output a string, the semicolon (;) cannot be displayed because the semicolon is used as a command delimiter in Bash. Use single or double quotes to output semicolons.
The code is as follows:
echo hello; hello #The first hello is treated as a string output, the second as a command
printf terminal printing
The parameters used by printf are the same as the printf function in C language, using quoted text or parameters separated by spaces. In the printf function, we can use the format string, specify the width of the string, left and right alignment, etc. By default, printf will not add a newline character at the end of the line, you need to add it manually.
The code is as follows:
printf "Hello world" #Use quote characters
printf "% -5s% -10s% -4s \ n" No Name Mark #Specify width and left alignment
printf "% -5s% -10s% -4s \ n" 1 Sarath 80.3456
working principle
% s,% c,% d,% f are all format replacement characters, and their corresponding parameters can be placed after the quoted format string.
% -5s indicates a string replacement with a left-aligned format and a width of 5.-indicates left-aligned. If no alignment is specified, the default is right-aligned. The width specifies the number of characters reserved for a variable. If the width of the content to be output is insufficient, it is filled with spaces. If it exceeds the specified width, the excess is discarded.
% 4.2f indicates that the specified decimal can retain two decimal places. It should be noted that 4 does not represent the number of integer parts, nor does it represent the sum of integer and decimal places.
Note: When using echo and printf command options, make sure that the option appears before all strings in the command line, otherwise Bash will treat the option as another string.
to add on
Use escape characters in echo
By default, echo will automatically add a newline at the end of the output string. You can use the -n option to ignore the newline at the end. echo also accepts escape sequences within double quoted strings as parameters. If you need to use an escape sequence, use the form echo -e "string containing escape sequence". In ubuntu14.04, you do not need to add the -e option. Part of the output.
The code is as follows:
echo "Hello World" #will add a newline at the end
echo -n "Hello Wordl" #No 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 amount of text. We can use escape sequences to achieve color output.
Each text color has a corresponding color code.
The code is as follows:
echo -e "\ e [1; 31m This is red text \ e [0m"
echo -e "\ e [1; 41m This is red background \ e [0m"
# ubuntu14.04 does not support this way to change the text or background color
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.