bash shell notes (1)

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

  1. method one:  
  2. Add a variable to the /etc/profile file [effective for all users (permanent)]  
  3. 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".  
  4. To make the changes just now take effect immediately, you need to execute the following code  
  5. # source /etc/profile  
  6.   
  7. Method Two:  
  8. Add variables to the .bashrc file in the user directory [effective for a single user (permanent)]  
  9. 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".  
  10. To make the changes just now take effect immediately, you need to execute the following code in the user directory  
  11. # source .bashrc  
  12.   
  13. Method three:  
  14. Directly run the export command to define variables [only valid for the current shell (BASH) (temporary)]  
  15. 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.  
  16. For example: export PATH=/usr/local/webserver/php/bin:$PATH 






Guess you like

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