Redshoe
Redshoe

Reputation: 189

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.

Upvotes: 1

Views: 68

Answers (2)

Cyrus
Cyrus

Reputation: 88889

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

Upvotes: 3

Redshoe
Redshoe

Reputation: 189

Ahh. I think I found a solution from here.

I would drop the last n characters by

${var%????}

Upvotes: 0

Related Questions