infoclogged
infoclogged

Reputation: 3997

IFS and bash code to read enter pressed by the user

Is there something wrong with this code

IFS=''
for i in `seq 1 20`; do
        sleep 1
        echo $((20-$i)) seconds till abort ... 
done

variable="dummy"
read -p "Enter to continue" -t 1 -N 1 variable
echo -e "\n"
while [ "$variable" != $'\x0a' ] 
do
read -p "Enter to continue" -t 1 -N 1 variable
echo -e "\n" 
done

The error is 20: syntax error in expression (error token is "2

If I remove the IFS='' then the countdown works fine, but then the part with "enter to continue" fails.

Is it advisable to put IFS='', coz I have seen that this variable is a delimiter for word boundaries?

Upvotes: 0

Views: 68

Answers (1)

William Pursell
William Pursell

Reputation: 212268

By setting IFS to the empty string you are only entering the loop once, and in the iteration of the loop the variable i is a 20 line string. $(()) doesn't know what to do with that string, so it tells you about the syntax error. (Note that the error token is 19 lines long, but you only paid attention to the first line.)

If you really need to manipulate IFS for other portions of your script (you probably don't!), you can do:

unset IFS
for i in $(seq 1 20); do
        IFS=''
        sleep 1
        echo $((20-$i)) seconds till abort ... 
done

Upvotes: 2

Related Questions