Search code examples
bashparameter-expansion

How to parse bash string from the first to nth to last character


Given a string Please help me do my homework !!!

I would like to parse it in to Please help me do my homework using bash.

What I tried is

str="Please help me do my homework !!!"
printf "${str: -4:100}"

which I get !!!. I guess ${str: -4} means it starts from 4th character from the last.


Solution

  • With GNU bash and its Parameter Expansion:

    str='Please help me do my homework !!!'
    echo "${str:0: -4}"
    

    Output:

    Please help me do my homework