Within the shell variable character substitution and variable character modification

 

we test.sh

= A 12,345,123 
# $ {a} in the first 123 replaced 321 
B = $ {A / 123 / 321 };
 echo  " echo variable A " 
echo $ A
 echo  " echo variable B " 
echo $ B 
A = 12,345,123 
the # $ {a} is replaced in all 123 321 
B = $ a { // 123/321}; 
echo  " echo variable a " 
echo $ a
 echo  " echo variable B " 
echo $ B


However, if you just see a $ {} can only be used delimited variable names, then you really misjudge the bash!

For completeness, we use here a few examples of some specific function to be described in $ {}:
Suppose we define a variable:
File = / dir1 / dir2 / dir3 / my.file.txt
we are replaced by $ {} different values obtained:
$ {# File * /}: remove first / and left string: dir1 / dir2 / dir3 / my.file.txt
$ {File ## * /}: the last removed a / its strings left: my.file.txt
$ # * File {.}: the first and remove the string on the left:. file.txt
$ ## * File {.}: remove the last a left and string: TXT
$ File% {/ *}: remove the last strip / right and string: / dir1 / dir2 / dir3
$ %% File {/ *}: remove first / :( null string and right)
$ File% * {.}: remove the string and the last right:. /dir1/dir2/dir3/my.file
$ %% File * {.} : the first and remove the string on the right:. / dir1 / dir2 / dir3 / my

Way to remember are:

# Is to remove the left (on the left side of the keyboard # $ sum)
% is to remove the right side (on the right side of the keyboard% of $)

Single symbol is the smallest match; two characters is the biggest match.

$ {file: 0: 5} : extracting the leftmost 5 bytes: / dir1
$ {File:. 5:}. 5: Extraction consecutive 5 bytes to the right of the fifth byte: / dir2

We can also do the replacement of variable values in a string:
$ {File / dir / path}: The first mention dir replaced path: /path1/dir2/dir3/my.file.txt
$ {// File dir / path}: dir provide all replaced path: /path1/path2/path3/my.file.txt

$ {} May use different variables for assignment state (is not set, null, non-null value):

$ {file-my.file.txt}: If $ file is not set, using as my.file.txt return value. (Non-treated and non-null value null value)
$ {File: -my.file.txt}: If $ file is not set to null, or is used as a return value my.file.txt. (Non-treated non-null value)
$ my.file.txt} + {File: $ file if null or empty value, are used as the return value my.file.txt. (Without processing is not set)
$ File {:} + my.file.txt: $ file if a non-null value, the return value as my.file.txt. (No processing time is not set and a null value)
$ File = {} my.file.txt: if $ file is not set, using as a return value my.file.txt while $ file assigned my.file .TXT . (Non-treated and non-null value null value)
$ {File: = my.file.txt}: If $ file is not set to null, or is used as a return value my.file.txt while $ file assigned to my.file.txt. (Not to deal with non-null value)
$ File my.file.txt {?}: If the $ file is not set, then my.file.txt output to STDERR. (Non-treated and non-null value null value)
$ {File:? My.file.txt}: $ file is not set, or if a null value, then output to my.file.txt STDERR. (Non-treated non-null value)

tips:
more than understanding that you have to make clear and unset null and non-null assignment of these three states.
In general,: null and about, do not bring: the words, null is not affected if the band: it is even null is also affected.


Oh well, $ {# var} variable length calculated value:
$ File} {# 27 obtained as /dir1/dir2/dir3/my.file.txt exactly 27 bytes ...

Guess you like

Origin www.cnblogs.com/faberbeta/p/linux-shell033.html