Parameter extension:
1. The parameter referenced by the name is called a variable
2. Parameters referenced by numbers are called positional parameters
3. Parameters referenced by a particular symbol have a special meaning and purpose, and are referred to as special internal variable references to bash.
Basic parameter extension:
The character $ will boot the parameter extension. Curly braces are optional, but curly braces protect the variable that is being extended, so that the content immediately following the curly brace is not extended.
Cases:
1 $ PARAMETER 2 $ {PARAMETER} 3 #如果参数名后跟其他字符, curly braces are required. 45 $ word=car6echo $WORDs
7 Echo ${word}s 8 Cars
For the arguments that follow the $9, you also use curly braces.
Note: Parameter names are case-sensitive.
Indirect parameter extension:
1 $ parameter=TEMP2 $ temp="It ' s indirect"3Echo $PARAMETER4 TEMP5echo ${! PARAMETER}6 it's Indirec
Case modification (Bash4.0):
1$Echo${parameter^}2 #将参数的第一个字符改为大写3$Echo${parameter^^}4 #将参数的全部字符改为大写5$Echo${parameter,}6 #将参数的第一个字符改为小写7$Echo${parameter,,}8 #将参数的全部字符改为小写9$Echo${paraneter~}Ten #将参数的第一个字符改为大写 One$Echo${paraneter~~} A#将参数的全部字符改为大写
Case Modification instance:
1 for file in *.txt2 > Do3 >mv"$file ""${file,,}"4 > Done
Variable name extension:
1 ${! prefix*}2 ${! [Email protected]} 3 #列出以字符串PREFI开头的所有变量名
Example: List all variable names that start with bash
1 $echo ${! bash*}2 BASH bashopts bashpid bash_aliases bash_argc bash_argv bash_cmds bash_command Bash_lineno Bash_source Bash_subshell Bash_versinfo bash_version
String removal:
1 ${parameter#pattern} 2 ${parameter# #PATTERN} 3 #移除从参数值的开头匹配指定模式的字符串 4 ${parameter%pattern}5 ${parameter%%pattern}6# Removing the string that matches the specified pattern from the end of the parameter value 7 # "#" and "%" means that the shortest text that matches the specified pattern is removed, and "# #" and "percent" represent the longest text to remove the matching pattern.
To remove an instance from a string:
1$ mystring="This is used for removing string"2$Echo${mystring#*}3is used forremovingstring4 5$Echo${mystring##*}6 string7 8$Echo${mystring% *}9This is used forremovingTen One$Echo${mystring%% *} AThis
Parameter usage, extract part of the file name:
1$ filename=Linux_bash.txt2 3$Echo${filename%.*} #移除文件名的后缀4 Linux_bash5 6$Echo${filename##*.} #移除文件名, keep the suffix7 txt8 9filename=/home/book/Linux_bash.txtTen One$Echo${filename%/*#移除文件名, keep the directory name A /home/book - - $ echo ${filename##*/#移除目录名, keep the file name theLinux_bash.txt
Linux Bash Shell Learning Notes