Frage

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 ?

War es hilfreich?

Lösung

${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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top