简体   繁体   中英

explain the linux regex for getting filename

Can any guys kindly help me explain why getting filename using this way. for example: the file is:

fileName = "/Users/test/am01/output/output.log"
fileName=${fileName##*/}

then we got

fileName = output.log

Could you guys help me explain what does ##* means, I'm so confused on this regex expression.

Additionally, there is another example like this:

filename="testdata.done"
echo ${filename%.done}
then we got test data

What does % mean, I've never seen this regex expression.

This is not reqexp, it it plain old bash magic. This guide says:

${string##substring}

Deletes longest match of $substring from front of $string.

The star is here a wildcard, so the */ means, delete the longest substring that ends with / .

Ie "/Users/test/am01/output/" in your case.

Part b of your question:

${string%substring}

Deletes shortest match of $substring from back of $string.

This is not regex !

This is BASH parameter expansion . From that reference:

There are also expansions for removing prefixes and suffixes. The form ${VAR#pattern} removes any prefix from the expanded value that matches the pattern. The removed prefix is the shortest matching prefix, if you use double pound-signs/hash-marks the longest matching prefix is removed. Similarily, the form ${VAR%pattern} removes a matching suffix (single percent for the shortest suffix, double for the longest).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM