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
  • 26,018

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
  • 26,018