You could do:
echo>&2 "Please enter a multiline string, Ctrl-D, twice if not on an empty line to end:"
input=$(cat)
printf 'You entered: <%s>\n'
Note that $(...)
strips all trailing newline characters, so that can't be used to input text that ends in newline characters.
To read exactly two lines, just call read
twice:
IFS= read -r first
IFS= read -r second
printf 'You entered: <%s\n%s>\n' "$first" "$second"
To input one line and have the \n
, \a
, \x45
, \0123
¹... sequences expanded in it:
IFS= read -r line
expanded_line=$(printf %b "$line")
printf 'You entered: <%s>\n' "$expanded_line"
Or with bash
/zsh
or recent versions of ksh93u+m, avoiding the stripping of trailing newline characters:
IFS= read -r line
printf -v expanded_line %b "$line"
printf 'You entered: <%s>\n' "$expanded_line"
In any case
¹ Beware that's the echo
style of expansion where octal sequences need a leading 0 (\0ooo
), not the usual \ooo
one as in the C language and as also recognised in the format argument of printf
.