How to remove last n characters from a string in Bash?

后端 未结 10 2519
后悔当初
后悔当初 2020-11-28 19:20

I have a variable var in a Bash script holding a string, like:

echo $var
\"some string.rtf\"

I want to remove the last 4 chara

10条回答
  •  青春惊慌失措
    2020-11-28 19:55

    In this case you could use basename assuming you have the same suffix on the files you want to remove.

    Example:

    basename -s .rtf "some string.rtf"
    

    This will return "some string"

    If you don't know the suffix, and want it to remove everything after and including the last dot:

    f=file.whateverthisis
    basename "${f%.*}"
    

    outputs "file"

    % means chop, . is what you are chopping, * is wildcard

提交回复
热议问题