How to create input read with newline in bash

14.04bashbashrccommand line

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

Best Answer

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]}.

Related Question