1. What is an environment variable
Environment variables are created by the system in advance, and play a very important role not only in Shell programming, but also in Linux system management.
For example, in the programming language we usually use, such as C language, we will all encounter the problem of variable scope . For example, variables defined in a function cannot be used outside the function . To be able to use this variable outside the function, the variable must be a global variable.
And the same is true for our environment variables, local variables and environment variables ( global variables ). When we program in the shell, such as vim xx.sh, the variables inside are written as local variables. Valid only in the shell that created it. And our environment variables are valid for the shell that created xx.sh and its derived subprocesses.
2. Where are the environment variables?
Generally exist in ~/.bashrc or /etc/profile files (scripts automatically called by the system)
List all environment variables in the system, you can use the env command
3. Commonly used environment variables
3.1 env command to view environment variables
Under the Shell, use the env command to view all the environment variables of the current user
All environment variables are displayed, it is not convenient to view, use grep to filter.
env|grep environment variable name
For example: View environment variables that contain PATH in the environment variable name.
3.2 PATH sets the search path for commands, separated by colons
3.3 HOME Current user home directory: /root
3.4 SHELL Current shell parser type: /bin/bash
3.5 HISTFILE displays the history list file of commands executed by the current user: /root/.bash_history
3.6 HOSTNAME Display the current host name: itheima
3.7 LANG default setting current system locale: zh_CN.UTF-8
3.8 HOSTTYPE Displays the architecture of the host, whether it is i386, i686, x86, x64, etc.: x86_64
Use environment variables, general environment variables are similar to global variables in C language, and can be used arbitrarily in shell script files
Examples are as follows:
output result
4. Custom setting environment variables
4.1. Temporarily set environment variables
What is a temporary environment variable, which means that when the terminal is closed or restarted, the temporary environment variable will be formatted.
4.2. Set temporary environment variables
(1) Example: Add temporary environment variables in the terminal
Print the environment variable results of env output
The environment variable exists the environment variable of SHELLMAY=999
Note: When we close the terminal, restart it, or open another terminal, the temporary environment variable just set will not exist.
4.3. Permanently set environment variables
You need to set it in the configuration file (~/.bashrc or ~/etc/profile). After setting, you need to use the
source command to configure the file to take effect immediately. Such as: source ~ /etc/profile Of course, the file /etc/profile can only be modified under root (super user).
(3) Example: Create a permanent ZYY=100
Step 1: sudo vim ~/etc/profile
Step two:
Step 3: Save and exit (if you don't use sudo, you can't save)
Step 4: source ~/etc/profile
Step 5: Output env
Hereby note:
When we open another terminal and use the env command to check, we cannot find the environment variable we just set.
The permanent environment variable just set does not really take effect, it just uses the source command to make it run temporarily.
Solution: restart, problem solved
get out of class is over! ! !