Score:0

How to escape to add additional lines in bash commands?

cf flag

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.

hr flag
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)
janos avatar
in flag
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
Score:0
vn flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.