0

I have this shell script which is supposed to run normally and stop when I press q button:

while true; do
    echo "hello"
    read -t 0.25 -N 1 input
    if [[ $input = "q" ]] || [[ $input = "Q" ]]; then
        break 
    fi
done

When I run it, I get this output that keeps being displayed continuously:

hello
script.sh: 3: read: Illegal option -t
script.sh: 4: script.sh: [[: not found
script.sh: 4: script.sh: [[: not found

But when I press on q button, it does not stop. It keeps running till I stop it using Ctrl + c.
Any suggestions to fix this?

1 Answer 1

3

I've just tried on my Ubuntu 16.04 system and it works correctly.

What I've found is, that if you use a generic sh instead of bash, it won't work giving out the error

read: Illegal option -t

Make sure that your script begins with #!/usr/bin/env bash or #!/bin/bash and not #!/bin/sh.

1
  • thank you so much, I remember that it worked for me once, but when I tried it now with a different PC, it didn't. Thank you.
    – singrium
    Sep 26, 2018 at 7:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .