2

I have this script:

#!/usr/bin/env bash
main() {
  while true; do 
    read -r -ep "> " input
    history -s "$input"
    echo "$input"
  done
}
main

which works well for single line strings.

Now I'm looking to allow the user to enter multiline strings, e.g. something like the following:

> foo \
> bar
foobar

how do I modify my read command to allow this functionality?

Foo
  • 232

1 Answers1

2

You explicitly disable the special handling of backslash with -r.

If you remove -r from your read invocation, you will be able to read your input with the escaped newline:

$ read input
hello \
> world
$ echo "$input"
hello world

Compare that with what happens if you use -r (which is usually what you want to do):

$ read -r input
hello \
$ echo "$input"
hello \

Note that without -r, you will have to enter \\ to read a single backslash.

Related:

Kusalananda
  • 333,661