Shell script topic-terminal printing, environment variables (2)

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.

echo

There are three ways to echo terminal output:
echo hello world!
echo "hello world!"
echo'hello world!'

Difference:
1. Because shell script is based; to separate the two statements, so echo + 文本This way can not be output with print; statement
2. To print special characters, such as the time, if you use echo + "text", then! Need to use \ to escape special characters;
3. To print and print special characters, use echo +'text' method, no need to use \ to escape.

printf

The usage of printf terminal output is similar to the printf function of C language, for example:
printf "hello world!"

You can use printf to format strings, for example:

#!/usr/bash

printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2s\n" 1 Tom 70.23
printf "%-5s %-10s %-4.2s\n" 2 chris 100.5655


Run the script, the effect is as follows:
Insert picture description here
Explanation:
%s, %c.%d are format replacement characters, and the usage is similar to C language.
%-5s specifies a string replacement with a left-aligned format and a width of 5 (- means left Alignment). If you don't use-specify the alignment, the string is right-aligned.

Escape newline characters in echo

echo -e "a string containing escape sequences"
For example, echo -e "1\t2\t3", the output is: 1 2 3 (if the -e option is not added, the output is 1\t2\t3)
Insert picture description here

Environment variable

Variables are an indispensable part of any programming language and are used to store all kinds of data. Scripting languages ​​usually do not need to declare their types before using variables, but can be directly assigned. Some special variables will be used by the shell environment and The operating system environment is used to store some special values, such variables can be called environment variables.
Variable assignment:
var=value (also var='value' or var="value")
Use variables:
in front of the variable Add $ to use, such as echo $var or echo $(var)

Variable example 1

Insert picture description here

Environment variable

Use the export command to set environment variables. When you enter export var = 123 in the terminal, the value of the variable var will always be defaulted to 123 in the current terminal (but after closing the terminal, the value of var cannot be used), you can use var directly The value of:
Insert picture description here
If you want a variable to be valid for all terminals, you can write the value of the variable to the ~/.bashrc file. For example,
edit the ~/.bashrc file and add the following statement at the end of the file:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/ros/kinetic/lib/x86_64-linux-gnu

Then we enter echo $LD_LIBRARY_PATH in the terminal, and the value just set will be displayed.

Some tips

1. Get the string length

length=${#var}
Insert picture description here

2. Identify the shell currently in use

echo $SHELL

3. Get the name of the executed script

echo $0
Insert picture description here

4. Check if you are a super user

UID is an important environment variable that can be used to check whether the current script is run by a super administrator or a normal user. For example:
Insert picture description here

Guess you like

Origin blog.csdn.net/hongge_smile/article/details/109171694