bash – How to remove the first line in a string variable?

Question:

How to remove the first line in a string variable? That is, the variable stores text in several lines. You need to remove the first line. Should be done without writing to a file

Answer:

yes as usual:

$ text=$(echo -e '1\n2\n3')
$ echo "$text"
1
2
3
$ echo "${text#*$'\n'}"
2
3

for reference: $ man bash , "Remove matching prefix pattern": ${parameter#word} .

well, or in the posix standard , "Remove Smallest Prefix Pattern": ${parameter#[word]} . and if this is relevant for your case (you don’t use something modern like bash/zsh ), then instead of $'\n' you will have to insert a “line feed” directly into the script text:

echo "${text#*
}"
Scroll to Top