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

后端 未结 10 2517
后悔当初
后悔当初 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:42

    You could use sed,

    sed 's/.\{4\}$//' <<< "$var"
    

    EXample:

    $ var="some string.rtf"
    $ var1=$(sed 's/.\{4\}$//' <<< "$var")
    $ echo $var1
    some string
    

提交回复
热议问题