0

When I run a bash program, it asks for input. Now I have to give a paragraph with newlines as input to the function.

In terminal whenever I hit Enter, it's going to next part of program. Which character should I use for escaping line - if I type \n it's taking the string \n as an input.

Artur Meinild
  • 21,605
  • 21
  • 56
  • 89
  • You should be able to use backslash characters to escape the literal newlines - see this related topic [Terminal shows > after entering \ .](https://askubuntu.com/a/1107974/178692) – steeldriver Dec 13 '22 at 13:06
  • Can you include the Bash script? It will help to know how it tries to read your multiline input, and how we can provide such input in a way that it will interpret it as intended – janos Dec 16 '22 at 04:17

1 Answers1

0

You can use sed to transform \n to actual newlines, like this:

#!/bin/bash

# Prints "Please enter a message",  and reads input
read -rep $'Please enter a message:\n' message

# Transforms any occurences of "\n" and replace them with newline
message=$(printf "${message}" | sed 's/\\n/\n/g')

# Prints the transformed string
echo "$message"

Result:

Please enter a message:
Paragraph\nMessage
Paragraph
Message

Inspired by this existing answer on Unix & Linux.

Artur Meinild
  • 21,605
  • 21
  • 56
  • 89