简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM