Вопрос

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