Regular expression explanation

What does the following regular expression mean?

fspec="/exp/home1/abc.txt"
fname="${fspec##*/}"

I know what he does, but not how it is done? Extracting fname is not clear to me.

Explain, please.

+3
source share
2 answers

The syntax ${var##*/}breaks everything to the last /.

$ fspec="/exp/home1/abc.txt"
$ echo "${fspec##*/}"
abc.txt

Generally ${string##substring}breaks the longest match $substringin front $string.

For further reference, you can, for example, check out Bash String Manipulation with a few explanations and examples.

+4
source

Below is an explanation from bash documentation.

${parameter#word}
${parameter##word}
The  word  is  expanded  to  produce  a pattern just as in pathname
expansion.  If the pattern matches the beginning of  the  value  of
parameter,  then  the result of the expansion is the expanded value
of parameter with the shortest matching pattern (the ``#'' case) or
the longest matching pattern (the ``##'' case) deleted.  

, word = */, () , /.

bash-3.2$fspec="/exp/home1/abc.txt"
bash-3.2$echo "${fspec##*/}"   # Here it deletes the longest matching pattern
# (i.e) /exp/home1/
# Output is abc.txt

bash-3.2$echo "${fspec#*/}"    # Here it deletes the shortest matching patter
#(i.e) /
# Output is exp/home1/abc.txt
bash-3.2$
+1

All Articles