Bash tutorial: Read input until input is blank


#!/bin/bash

# PURPOSE: Grabs text from user one line at a time
# until the input is null (user typed nothing).
# Does something each time with the user input (echo)

echo "Enter something. It will be echoed."
echo "If you enter nothing, the program will end."

read TEST
echo "output: $TEST"

while [ $TEST ]
do

read TEST

if [ $TEST ]; then
echo "output: $TEST"
fi

done

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.