I'm trying to take the input from the user into an array but Shell is accepting user input separated by spaced. Is there any way to accept the user input given separately in each line. My code below:
#!/bin/bash
echo "enter the servers names..."
read -a array
for i in "${array[@]}"
do
echo $i
done
exit 0
Input:
hello world
I want to the input to be taken as below (in two different lines):
hello
world
Kindly help. Thanks.
IFS=$'\n'before yourreadto only break on newlines. (you can save the old value, e.g. (oldIFS=IFSbefore changing it, the restore the originalIFS=oldIFSafter the loop -- not necessary here because you justexit)IFS=$'\n' read -a array?tr -s ' ' '\n' | read -a ..., and be aware that this will make it so the variables are only available within the subshell created by the pipe. This will also take care of blank lines for you (unless that blank line is at the start of the input).