Score:0

how to create input read with newline in bash?

us flag

I want to ask

I have a problem, how do I get input in bash to do newlines?

read -p "List Name: " list

cat <<EOF >names.txt
List Names:
$list

EOF

i can not do a new line or use the command \n , how to add a new line command ?

I want result output names.txt like this

List Name :
    Robert
    James
    Samuel
FedKad avatar
cn flag
Please be more specific, [edit] your question and provide a sample input and expected output.
Score:2
in flag

If you want a list with one item per line, you can use readarray:

# Read list
echo "Enter one name per line, finish with Ctrl-D:"
readarray -t list

# Use list as normal array
echo "Name List:"
printf '%s\n' "${list[@]}"

Now you can use list as normal array, e.g. ${list[1]}.

Score:1
sa flag

By default bash uses a space character as a delimiter to separate words. This shell script uses a space character as a delimiter to separate three names that are input by the user. Paste the following shell script into a text file named input-names.sh, right click input-names.sh, select Properties -> Permissions tab and put a checkmark to the left of Allow executing file as a program.

#!/bin/bash

# Read multiple inputs
echo "Type three names separated by space characters."
read name1 name2 name3
echo "List name :"
echo "    $name1"
echo "    $name2"
echo "    $name3"

The following output will appear after executing the above script.

:~$ ./input-names.sh
Type three names separated by space characters.
Robert James Samuel
List name :
    Robert
    James 
    Samuel 
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.