The variable function of the shell
The role of variables is that when you write a large script, the path appears more often in the script. If you change the host next time, it will be very troublesome to modify all the paths in the script. If you use variables, write the definition of the variable in The first and the following path names are replaced with variables, and just modifying one line is equivalent to modifying the entire script.
1. Obtaining variables: use the echo command
echo $PATH : Display the set path. Or write echo ${PATH}
2. Set and modify variables
echo $MYNAME
MYNAME=Jason
echo $MYNAME
unset MYNAME unset variable
3. Expand the content of the variable
name="$name"jason with double quotes
4. Display the core version
uname -r
5. View variables in the shell environment
env、set
Note: The variables set in the above methods are custom variables, not environment variables, and they disappear after restarting.
How to make the variable take effect permanently: the following is a summary for others
- method one:
- Add a variable to the /etc/profile file [effective for all users (permanent)]
- Use VI to add a variable in the file /etc/profile file, the variable will be valid for all users under Linux and is "permanent".
- To make the changes just now take effect immediately, you need to execute the following code
- # source /etc/profile
- Method Two:
- Add variables to the .bashrc file in the user directory [effective for a single user (permanent)]
- Use VI to add variables to the .bashrc file in the user directory. The changes are only valid for the current user and are "permanent".
- To make the changes just now take effect immediately, you need to execute the following code in the user directory
- # source .bashrc
- Method three:
- Directly run the export command to define variables [only valid for the current shell (BASH) (temporary)]
- Use [export variable name=variable value] to define a variable directly on the shell command line. The variable is only valid under the current shell (BASH) or its subshell (BASH). When the shell is closed, the variable is invalid. , when you open a new shell, there is no such variable. If you need to use it, you need to redefine it.
- For example: export PATH=/usr/local/webserver/php/bin:$PATH