Friday, May 3, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2279  / 2 Years ago, mon, march 28, 2022, 3:09:12

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
, how to add a new line command ?


I want result output names.txt like this


List Name :
Robert
James
Samuel

More From » command-line

 Answers
7

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
' "${list[@]}"

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


[#808] Tuesday, March 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fishutt

Total Points: 391
Total Questions: 137
Total Answers: 106

Location: Mexico
Member since Tue, Aug 11, 2020
4 Years ago
fishutt questions
Thu, Nov 17, 22, 07:36, 1 Year ago
Wed, Feb 8, 23, 06:50, 1 Year ago
Thu, Aug 25, 22, 13:26, 2 Years ago
Fri, Apr 22, 22, 00:08, 2 Years ago
Wed, May 26, 21, 00:38, 3 Years ago
;