Ursescu Ionut
Ursescu Ionut

Reputation: 97

sed on a Bash string variable

I have a variable:

temp='Some text \n Some words \n'

I would like to delete some of those lines with sed:

sed -e '1d' $temp 

I want to know if this is possible.

Upvotes: 3

Views: 1767

Answers (2)

codeforester
codeforester

Reputation: 42999

When you pass your string as an argument, sed would interpret as a filename or a list of file names:

sed -e '1d' "$temp"

Of course, that's not what you want.

You need to use here string <<< instead:

temp=$'Some text\nSome words\nLast word'
sed '1d' <<< "$temp"

Output:

Some words
Last word

Upvotes: 4

Samuel Kirschner
Samuel Kirschner

Reputation: 1185

posix-compatible way to do this is:

temp="$(printf '%s\n' "$temp" | sed '1d')"

if you only need a bash-compatible solution see codeforester's answer, as the here string syntax is much better readable.

Upvotes: 0

Related Questions