2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
$
$()
UsageSpecial shell variables, used in scripts, functions, etc. to pass parameters
There are special, positional parameter variables as follows
variable | effect |
---|---|
$0 | Get the shell script file name and script path |
$n | Get the nth parameter of the shell script, n is between 1 and 9, if it is greater than 9, some{} ,like{$10} |
$# | Get the total number of parameters after the executed shell script |
$* | Get all the parameters of the shell script. Without quotes, it is equivalent to$@ The effect of adding quotation marks "$*" isReceives all parameters as a single string |
$@ | Without quotation marks, the effect is the same as above. With quotation marks,Receives all parameters as separate strings |
Search method:man bash
,searchSpecial Parameters
variable | effect |
---|---|
$? | The return value of the last command execution status, 0 for success, non-0 for failure |
$$ | The process number of the current shell script |
$! | PID of the last background process |
$_ | Get the last parameter of the last command |
-n
: Output content without line break-e
: Interpret escape characters n
: Line breakr
: Entert
:tabb
:backspacev
: vertical tab charactereval ls; cd ~
exec
After execution, automaticallyexit
grammar:
grammar | effect |
---|---|
# | Remove the shortest match from the beginning |
## | Remove the longest match from the beginning |
% | Remove the shortest match from the end |
%% | Remove the longest match from the end |
# | Specify character content interception |
a*c | Matches a string that starts with a, has any characters in the middle, and ends with c |
${变量} | Returning the value of a variable |
${#变量} | Returns variable length, character length |
${变量:start} | Returns the character after the value of the variable offset |
${变量:start:length} | Extract the length-limited characters after offset |
${变量#word} | Delete the shortest matching word substring from the beginning of the variable |
${变量##word} | Delete the longest matching word from the beginning of the variable |
${变量%word} | Delete the shortest matching word substring from the end of the variable |
${变量%%word} | Delete the longest matching word substring from the end of the variable |
${变量/pattern/string} | Replace the first match of pattern with string |
${变量//pattern/string} | Replace all patterns with string |
Operation Example: Delete the file name
# 通过⼦串的替换⽅式
snowk@SnowKYP# f=stu_102999_1_finished.jpg
# 变量的⼦串功能,去掉后缀
snowk@SnowKYP# echo ${f//_finished/}
stu_102999_1.jpg
# 利⽤变量的反引⽤替换⽂件名
snowk@SnowKYP# mv $f `echo ${f//_finished/}`
# 剩下的⽂件,利⽤循环操作
# 找出剩下所有需要替换的jpg⽂件
snowk@SnowKYP# ls *fin*.jpg
stu_102999_2_finished.jpg stu_102999_3_finished.jpg stu_102999_4_finished.jpg
stu_102999_5_finished.jpg
# 写shell循环代码,循环操作
# 去掉所有jpg⽂件的_finished后缀
snowk@SnowKYP# for file in `ls *fin*.jpg`;do mv $file `echo ${file//_finished/}`;done
snowk@SnowKYP# ls *.jpg
stu_102999_1.jpg stu_102999_2.jpg stu_102999_3.jpg stu_102999_4.jpg stu_102999_5.jpg
parameter,参数,范围
${parameter:-word}
:ifparameter
The variable value is empty, returnword
String${parameter:=word}
:ifparameter
If the variable value is empty,word
Substitutes a variable value and returns its value${parameter:?word}
:ifparameter
The variable value is empty.word
Asstderr
Output, otherwise output variable value ${parameter:+word}
:ifparameter
If the variable value is empty, do nothing.word
return# 原脚本命令,有bug,有歧义
find ${file_path} -name '*.tar.gz' -type f -mtime +7 | xargs rm -f
# 修正后的脚本命令
# 如果有bug起义,就在当前目录搜索并执行删除
find ${file_path:=/tmp} -name '*.tar.gz' -type f -mtime +7 | xargs rm -f