Linux Bash Shell learning notes, bash learning notes
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 specific symbol have special meanings and purposes, and are referred to as Bash's special internal variable reference.
Basic Parameter extension:
Character $ will guide parameter extension. Braces are optional, but braces can protect the variables to be extended so that the content following the braces will not be extended.
Example:
1 $ PARAMETER2 $ {PARAMETER} 3 # braces are required if the PARAMETER name is followed by other characters. 4 5 $ WORD = car6 $ echo $ WORDs
7 $ echo $ {WORD} s8 cars
Parameters after $9 must also be in braces.
Note: The parameter name is case sensitive.
Indirect parameter extension:
1 $ PARAMETER=TEMP2 $ TEMP="It's indirect"3 $ echo $PARAMETER4 TEMP5 $ echo ${!PARAMETER}6 it's indirec
Modify the case (Bash4.0 ):
1 $ echo $ {PARAMETER ^} 2 # change the first character of the PARAMETER to uppercase 3 $ echo $ {PARAMETER ^} 4 # change all characters of the PARAMETER to uppercase 5 $ echo $ {PARAMETER ,} 6 # change the first character of the PARAMETER to lowercase 7 $ echo $ {PARAMETER,} 8 # change all characters of the PARAMETER to lowercase 9 $ echo $ {PARANETER ~} 10 # change the first character of the parameter to uppercase 11 $ echo $ {PARANETER ~~} 12 # change all characters of the parameter to uppercase
Modify instance case:
1 # for file in *.txt2 >do3 >mv "$file" "${file,,}"4 >done
Variable name extension:
1 $ {! PREFIX *} 2 $ {! PREFIX @} 3 # list all variable names starting with a string PREFI
Example: list all variable names starting 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 # Remove a string matching the specified mode from the beginning of the PARAMETER value 4 $ {PARAMETER % PATTERN} 5 $ {PARAMETER % % PATTERN} 6 # Remove the string matching the specified PATTERN from the end of the parameter value 7 # "#" and "%" indicates removing the shortest text matching the specified PATTERN, '##' and '%' indicate removing the longest text in the matching mode.
String removal instance:
1 $ MYSTRING="This is used for removing string" 2 $ echo ${MYSTRING#* } 3 is used for removing string 4 5 $echo ${MYSTRING##* } 6 string 7 8 $echo ${MYSTRING% *} 9 This is used for removing10 11 $echo ${MYSTRING%% *}12 This
Parameter purpose. Extract part of the file name:
1 $ FILENAME=linux_bash.txt 2 3 $ echo $ {FILENAME %. * }# remove the FILENAME suffix 4 linux_bash 5 6 $ echo $ {FILENAME ##*.} # Remove the file name, retain the suffix 7 txt 8 9 FILENAME =/home/book/linux_bash.txt10 11 $ echo $ {FILENAME %/*} # Remove the file name, keep directory name 12/home/book13 14 $ echo $ {FILENAME ### */} # Remove directory name, keep file name 15 linux_bash.txt