Assigning values to variables
#定义变量, note that there is no space on either side of the equal sign variable=
#定义环境变量
Export variable=
#双引号: Can contain spaces, can escape special characters variable=""
#$ () or anti-quote: assigns a command result to a variable variable=$ () variable= "
#将变量值赋值给变量gg= $aa $bb
The value of the variable
#取值
$variable
#标准写法, it is recommended that you use this method
#DAY=
#Echo
Echo " Hello Sunday ">
Ls
Echo " Hello Sunday ">
Ls
Cancellation and viewing of variables
#unset: Canceling variables and functions
unset variable
unset function
#env View all of the system's environment variables #set View all system preset variables + user-defined variables
Local Variables/user variables
Local variables: Valid only in the current shell;
User variables: Available for use with the current user,. BASHRC can be viewed.
Global variables/environment variables
The shell that currently creates the variable is the parent shell, and global variables can be passed from the parent shell to the child shell, Sun Shell
Note that environment variables can only be passed down and cannot be passed up.
Some common preset variables (can be viewed by Man bash):
#变量BASH: Show current bash Shell[[email protected] shell]#Echo$BASH/bin/bash# variable bash_version: Displays the current bash shell version [[email protected] shell]#Echo$BASH _version4.1.2(1)-release# variable hostname: hostname [[Email protected]_host test]#Echo$HOSTNAMETXY _host# variable hosttype: host architecture [[Email Protected]_host test]#Echo$HOSTTYPEx 86_64# variable machtype: GNU identity for host type [[Email protected]_host test]#Echo$MACHTYPEx 86_64-redhat-linux-gnu# variable lang: The language environment of the system [email protected]_host test]#Echo$LANGC # set locale to Chinese # export LANG=zh_cn. utf-8#变量PWD: Current directory [[email protected]_host test]#Echo$PWD/shell/test# variable oldpwd: Last access directory [[email protected]_host test]#Echo$OLDPWD/shell# variable euid: Displays the uid[[email of the current user protected]~]#Echo$EUID0#变量CDPATH: You can quickly enter this directory #cd when you first see if the current directory has a network-scripts, if not, enter the directory defined by Cdpath [[email protected] shell]# Cdpath="/etc/sysconfig"[[Email protected] shell]# CD network-Scripts/etc/sysconfig/network-scripts# variable funcname: Used within the function to display the current function name [[Email protected]_host test]#CatFuncName.SH#/bin/Bashfuncname () {Echo$FUNCNAME}funcname[[email protected]_host test]# bash FUNCNAME.SHfuncname
Special variables
1. Position parameters
$: Save the actual script name; This value is available regardless of whether the script has parameters.
$1-$9: 第1-9个 parameter
${10}: Representation after position parameter is greater than 9
$#: Sum of the number of script parameters
$*/[email Protected]: represents all parameters of the script
2. Command return value
After executing a command,
$? A value of 0 indicates a successful execution, and a non-0 indicates an execution failure.
[Shell base]--variable