The error message suggests that you are in fact not executing the script using bash
.
Either make the script executable and add a proper #!
-line on the first line of the script, e.g.
#!/bin/bash
Or, execute the script with bash
explicitly:
$ bash script.sh
You should treat sh
and bash
as implementing separate languages, and use the correct interpreter for the script you're writing. In this case, you're using read
with the -u
option. This is originally a ksh
extension to the standard read
specification, and it's also implemented in bash
and zsh
. Hence, you need to run the script with bash
, zsh
or ksh
.
How to know when to use sh
and when to use bash
or some other shell? It's simple, you learn the way sh
works and what other features other shells add.
bash
.ksh
andzsh
supportread -u <fd_num>
. – cuonglm Dec 09 '16 at 03:22/usr/bin/env
is relevant here. Ubuntu/bin/sh
is linked to/bin/dash
, so you may want to use/bin/bash
instead. – cuonglm Dec 09 '16 at 03:25sh
(e.g.sh myscript
) then that will override the shebang line. Just make the file executable and run it by its path + name./myscript
. See What is the difference between running “bash script.sh” and “./script.sh”? – steeldriver Dec 09 '16 at 03:38./script
should do it – Jeff Schaller Dec 09 '16 at 16:20