我有一個變量:sed的一個猛砸字符串變量
temp='Some text \n Some words \n'
我想刪除其中的一些線與sed的:
sed -e '1d' $temp
我想知道這是可能的。
我有一個變量:sed的一個猛砸字符串變量
temp='Some text \n Some words \n'
我想刪除其中的一些線與sed的:
sed -e '1d' $temp
我想知道這是可能的。
當你通過字符串作爲參數,sed
將其解釋爲一個文件名或文件名列表:
sed -e '1d' "$temp"
當然,這不是你想要的。
這裏需要使用字符串<<<
代替:
temp=$'Some text\nSome words\nLast word'
sed '1d' <<< "$temp"
輸出:
Some words
Last word
請注意'\ n'因爲'$'\ n''引用而只是一個換行符。只有'\ n'',它將是一個字面的'\ n'。 –
請參見[Bash中的單引號和雙引號之間的區別](https://stackoverflow.com/a/42082956/6862601)。 – codeforester
POSIX兼容的方式來做到這一點是:
temp="$(printf '%s\n' "$temp" | sed '1d')"
如果你只需要一個bash兼容解決方案請參閱codeforester's answer,因爲這裏的字符串語法要好得多dable。
顯示預期結果 – RomanPerekhrest