質問

I found the following "trick" to get the file name and its extension.

filename="my file.ext"
name=${filename%.*}
ext=${filename#*.}

Could you explain those ${filename%.*} and ${filename#*.} things and give a link to the relevant documentation ?

役に立ちましたか?

解決

${filename%.*}   
# % Deletes shortest match of pattern ".*" from back of "filename".

${filename#*.}   
# Deletes the pattern '*.' from front of filename i.e. removes the name

The Linux Documentation project has this page on Manipulating Strings. Go down to the section marked substring removal

他のヒント

These are both a form of "Remove matching prefix pattern" see bash man page and search for Remove matching prefix pattern.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top