String operations, functions, and variable scope string operations in shell notes are too common. Shell code #! /Bin/bash # string truncation #1 use a simple command to intercept basename dirname # dirname and return another path discarded by basename. Echo 'basename/usr/local/share/doc/foo/foo.txt is: 'basename/usr/local/share/doc/foo/foo.txt echo 'basename/usr/local/share/doc/foo/foo.txt is: 'basename/usr/local/share/doc/foo/foo.txt www.2cto.com #2 truncation of myvar1_foodforthought.jpg echo 'myvar = '$ {myvar} and' $ {myvar # * fo} is: 'echo $ {myvar # * fo} # enter the variable name in $ {}, two ##, and three wildcards ("* fo") # bash first obtains myvar, locate the start point of the string "foodforthought.jpg" and match the wildcard "*. f O ", and then cut it from the start of the string. Echo '$ {myvar # * fo} is: 'echo $ {myvar # * fo} # Only one #. bash removes the shortest match from the start string. # When searching for the longest match, use ##( because # ratio # Is long); when searching for the shortest match, use #. Myfoo = "chickensoup.tar.gz" echo 'myvar = '$ {myfoo} and' $ {myfoo %. *} is: 'echo $ {myfoo %. *} echo '$ {myfoo %. *} is: 'echo $ {myfoo %. *} # except to remove the matching wildcard from the end of the string, the extension options of % and % variables are the same as those of # And #. #3. Select a specific substring based on the specific character offset and length (separated by a colon. # Bash must be used to explain the execution; otherwise, the following error occurs: bash string. sh exclatm = cowabunga echo exclatm =$ {exclatm}, '$ {exclatm: 0: 3} is: 'echo $ {exclatm: 0: 3} echo exclatm =$ {exclatm}, '$ {exclatm:-} is: 'echo $ {exclatm:-} echo' $ {exclatm: 3: 7} is: 'echo $ {exclatm: 3: 7} # String length $ {# parameter} var = "hello world" echo length of \ "$ var \" is: $ {# var }#$ {parameter/pattern/string} shell extends pattern as the file name extension, and replaces the longest matching mode (If the matching mode exists ). To match the pattern at the beginning of the extended value of parameter, prefix # is attached. to match the pattern at the end of the value, prefix % is attached. If the string is null, the/at the end of the string may be ignored, and the matching will be deleted. Use @ or $ to replace each parameter in the list. # $ {Parameter // pattern/string} replaces all matches (not the first match. X = "a1 b1 c2 d2" echo '$ {x/1/3} is:' $ {x/1/3} echo '$ {x // 1/3} is: '$ {x // 1/3} echo' $ {x //? 1/3} is: '$ {x //? 1/z3} the function can also be defined in www.2cto.com shell, and the function can be called like a script. Shell code #! /Bin/bash echo '$ 2' ": $2" tarview () {echo-n "displaying contents of $1" postfix =$ {1 ##.} case $ postfix in tar) echo "(uncompressed tar)" tar tvf $1; # End of a branch gz) echo "(gzip compressed tar)" tar tzvf $1 ;; bz2) echo "(bzip2-compressed)" cat $1 bzip2-d | tar tvf-; *) echo "unhandle type. "; esac echo" tarview $2 "} tarview myfun () {echo" myfun $1, $2 "} # If no parameter is passed when a function is called, reference the script variable. # The parameters in the function are similar to the variables that the script receives from the command line. $ n (n> 0) indicates the nth variable; #$0 will be extended to the string bash (if a function is run interactively from the shell) or the name of the script that calls the function. Myfun abc # namespace. In bash, an environment variable is added to a global namespace every time it is created inside a function. Myvar = hello myfunc () {myvar = "one two three" for x in $ myvar do echo $ x done} myfunc echo $ myvar $ x www.2cto.com # use the local keyword to define the local variable myvar = hello localFunc () {echo '$ 0' ": $0" local x local myvar = "one two three" for x in $ myvar do echo $ x done} localFunc echo $ myvar $ x