Variable definition
1. What is a variable
Variable is the address of an area of memory
2. The meaning of variable existence
The command cannot operate the constantly changing target
Using a fixed string of characters to represent an unfixed target can solve this problem
The method of defining variables in Shell
The environment level
export a=1
will be invalid after the environment is closed
Export a=1 in the first shell, it can take effect at this time
When viewing in the new environment (in the second shell), the variable definition is invalid
The user level is only valid for the set user
vim ~/.bash_profile
export a=1
vim ~/.bash_profile
source ~/.bash_profile At this point, after executing the source command in the two shells, the variables can be effective
But cannot be used when switching to another user
Everyone at the system level can use
vim /etc/profile
export a=1
Variable names defined
using "character", " ", "digital" and can not start with a number
recommendations:
short variable names, all uppercase character
variable names long, with " molecular class" area
such as:
WESTOS
Westos_dd
Westos_ss
Translation and declaration of variables
Translate
\ #转翻译 single character
“” #Weak quotation, batch interpreting characters cannot be translated “” “`” “$” “!”
'' #strong quotation
Statement
a = 1
echo $ ab
echo $ {a} b
a=( ls -l file
) #define a as the command
echo $a
echo ${a[0]}
#View the 0th column of the command echo ${a[3]}
echo ${a[-1]} #View the countdown of the command The first column
echo ${a[*]} #View all columns of the command (the same below)
echo ${a[@]}
Alias setting in Linux commands
alias sss='cat' #temporary setting
Set in environment 1, available
Cut to environment 2, gg
vim ~/.bashrc
alias sss='cat' #Valid only for users
vim /etc/bashrc
alias sss='cat' #Valid for all users of the system
unalias sss #Delete the alias in the current environment
User environment variable changes
Environment variable:
the command search path used by the user in the operating system in real time
Setting method:
~/.bash_profile #User-level
export PATH=$PATH:/mnt
/etc/bash_profile #System-level
export PATH=$PATH:/mnt
At this time, the script can be executed without adding an absolute path
Passing parameters in script
非交互模式:
$0 is test.sh $0 是脚本本身
$1 is westos $1 是脚本后输入的第一串字符,2、3...以此类推
$2 is linux
$3 is sss
$# is 3 # 指脚本后输入的所有字符串个数
$* is westos linux sss $* 指脚本后所输入的所有字符“westos linux sss”
$@ is westos linux sss $@ 指脚本后所输入的所有字符"westos" "linux" "sss"
∗ and * and ∗ Difference from @: