Extract filename and extension in Bash

后端 未结 30 2030
野趣味
野趣味 2020-11-21 05:29

I want to get the filename (without extension) and the extension separately.

The best solution I found so far is:

NAME=`echo \"$FILE\" | cut -d\'.\'          


        
30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:30

    Simply use ${parameter%word}

    In your case:

    ${FILE%.*}
    

    If you want to test it, all following work, and just remove the extension:

    FILE=abc.xyz; echo ${FILE%.*};
    FILE=123.abc.xyz; echo ${FILE%.*};
    FILE=abc; echo ${FILE%.*};
    

提交回复
热议问题