"Shell Programming Practice" Chapter 3 Shell Variable Fundamentals (1)

1. What is a shell variable
Simply put, a variable is a fixed string that replaces more and more complex content, which may also contain variables, paths, strings, and other content. A variable is a place for temporarily storing data and a data marker. The stored data exists in the memory space, and the data corresponding to the variable can be retrieved by calling the name of the variable in the memory space correctly.
Significance: The biggest advantage of using variables is to make program development more convenient. Of course, it is also necessary to use variables in programming, otherwise it will be difficult to complete related program development work.
2. How to use variables (assignment)
[root@thzzc1994 ~]# oldboy="I am oldboy"
[root@thzzc1994 ~]# echo $oldboy
I am oldboy
The content of the variable should generally be double-quoted to prevent errors, especially When there are spaces between the contents in the value.
3. Variable type
Variables can be divided into two categories: environment variables (also called global variables) and ordinary variables (also called local variables).
Environment variables, used in the shell that created them and any subprocess shells that spawn them. Environment variables can be divided into custom and bash built-in.
Ordinary variables, which can only be used in the shell function or shell script that created them. Ordinary variables are generally created by developers when developing script programs.
4. Environment variables
Environment variables can be set and created on the command line, but these variable values ​​will be lost when the user exits the command line.
If you want to save environment variables permanently, you can define them in .bash_profile or .bashrc in the user's home directory, or in the global configuration /etc/bashrc or /etc/profile.d.
Some environment variables, such as HOME, PATH, SHELL, UID, USER, etc., are already defined by the /bin/login program before the user logs in.
Usually environment variables are kept in ~/.bash_profile or /etc/profile.
Three methods for setting environment variables:
(1) export A=value
(2) declare -x A=value
(3) A=value; export A/;
methods for setting environment variables to take effect permanently:
(1) User environment variables: ~/.bashrc and ~/.bash_profile
(2) Global environment variables: /etc/bashrc, /etc/profile and /etc/profile.d
Tips: (1) Pay attention when writing crond timed tasks, the scripts used Environment variables are best first redefined in the shell script being executed.
(2) If you want the environment variable to take effect permanently, you can put it in the user environment variable file or the global environment variable file.
5. Local variables
Local variables are used in scripts in the user's current shell lifetime. If another process is started or exited in the shell, the value of the variable will have no effect.
Three ways to define local variables:
variable = value
variable = 'value'
variable = "value"
single quote'', double quote "" and the difference between unquoted
'': what you see is what you get
"": parsing the variable value, Re-output
without quotation marks: Parse the variable value and then output, but it must be continuous.
In summary, it is generally recommended to use double quotation marks instead of no quotation marks.
6. Three ways for the system to run the shell:
(1) System login default shell
(2) Non-login interactive shell (such as bash, ssh)
(3) Script execution non-interactive shell
login: /etc/profile=>/ etc/profile.d=>/etc/sysconfig/i18n
non-login: ~/.bashrc=>/etc/bashrc
7. Command definition variable
(1) date
[root@thzzc1994 ~]# time= date
[root@thzzc1994 ~] # echo $time
Tuesday, May 01, 2018 10:43:51 CST
(2) $(date)
--recommended [root@thzzc1994 ~]# time=$(date)
[root@thzzc1994 ~]# echo $ time
Tue May 1, 2018 10:44:04 CST

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325458757&siteId=291194637