Variables in Shell (definition variables, variable translation and declaration, command alias setting, environment variable changes, parameter passing in scripts)

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
Insert picture description here

When viewing in the new environment (in the second shell), the variable definition is invalid
Insert picture description here

The user level is only valid for the set user
vim ~/.bash_profile
export a=1

vim ~/.bash_profile
Insert picture description here

source ~/.bash_profile At this point, after executing the source command in the two shells, the variables can be effective
Insert picture description here
Insert picture description here
Insert picture description here

But cannot be used when switching to another user
Insert picture description here

Everyone at the system level can use
vim /etc/profile
export a=1

Insert picture description here
Insert picture description here

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
Insert picture description here
Insert picture description here

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[@]}
Insert picture description here
Insert picture description here

Alias ​​setting in Linux commands

alias sss='cat' #temporary setting

Set in environment 1, available
Insert picture description here

Cut to environment 2, gg

Insert picture description here

vim ~/.bashrc
alias sss='cat' #Valid only for users
Insert picture description here
Insert picture description here

vim /etc/bashrc
alias sss='cat' #Valid for all users of the system

unalias sss #Delete the alias in the current environment
Insert picture description hereInsert picture description here

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
Insert picture description here

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"

Insert picture description here

∗ and * and Difference from @:

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42958401/article/details/108467479