Damon Hill
Damon Hill

Reputation: 136

How to remove a substring matching a pattern in a specific position in bash?

In a bash script, I use a $temp variable containing several lines of text and I need to remove the first character in all the lines that start with a space.

I tried using sed:

temp=$(sed 's/ //' <<< "$temp")

but it removes the first space no matter where it is, so I end up with some words altogether.

Upvotes: 0

Views: 330

Answers (1)

Jotne
Jotne

Reputation: 41460

Try this then:

temp=$(sed 's/^ //' <<< "$temp")

It will anchor the space to beginning of the line.

Upvotes: 2

Related Questions