How can I resolve this error in shell scripting: “

2019-07-02 06:08发布

问题:

#!/bin/bash
echo -n "Hurry up and type something! > "
if read -t 10 response ; then
echo "Greate, you made it in time!"
else
echo "sorry, you are too slow!"
fi

I have written above code in terminal and got error "read: Illegal option -t".

回答1:

Bash supports -t, so it looks like you're trying to execute it with sh or some other shell, which is odd, since you have the correct shebang.

Make sure you run it with ./script or path_to_script/script. If you just run it in the terminal, first start bash.



回答2:

bash supports the -t option for the read builtin since version bash-2.04 (see ChangeLog), so either you are using an ancient version of bash (<= 2.03) or are not really running your script under bash.

Run bash --version to check the version and double-check that your shebang really looks like #!/bin/bash in your script.