First, shell variables
1. Variables can be divided into two categories: environment variables (global variables) and local variables.
Environment variables: All environment variables are system global variables that can be used in all child processes, including editors, shell scripts, and various applications.
Environment variables can be set at the command line, but they are also lost when the user exits, so it is best to use the. bash_profile file in the user's home directory or the global configuration
Defined in/ETC/BASHRC,/etc/profile files, or/etc/profile.d/. The environment variables are placed in the profile file and are initialized each time the user logs on.
All environment variables are uppercase.
Before the environment variable is applied to the user process, the Export command or source or "." Must be used Command Export.
See some Linux common system environment variables through Baidu.
Common environment variables:
(1) Modify history command to display historical command record number: Histfilesize=5
Histsize=5
(2) To view the type of system file: $HOSTTYPE
(3) View system hostname: $HOSTNAME
Local variables:
Define variables: (1) Variable name =value
(2) Variable name = ' value ', using the contents of single quotes is what is, inside if there is a variable is not parsed.
(3) Variable name = "value", using double quotation marks in the content, if there are variables will parse the variable into the content to display
Exam: a = 192.168.1.2
b = ' 192.168.1.2 '
c = "192.168.1.2"
A = 192.168.1.2-$a
b = ' 192.168.1.2-$a '
c = "192.168.1.2-$a"
echo "a = $a"
echo "B = $b"
echo "C = ${c}"
Note: $a is the same effect as ${c}
Answer: a = 192.168.1.2-192.168.1.2
b = 192.168.1.2-$a
c = 192.168.1.2-192.168.1.2-192.168.1.2
There are two ways to use variables as content:
(1) cmd = ' date +%F '
(2) cmd = $ (date +%F)
When do I use the ${var} with this curly brace scenario?
such as: TAR-XZVF fcz_$pwd_log.tar.gz, the system does not know whether the PWD as a variable or pwd_log as a variable
At this point, if you use curly braces TAR-XZVF fcz_${pwd}_log.tar.gz explicitly tells the system that PWD is a variable, and pwd_log is not a variable.
Shell Primer Basics