2

I am trying to use read, to read from a file descriptor, like so:

read -u fd

as in in this link.

Here is the code I am using in a bash script:

MESSAGE=$(read -u $NODE_CHANNEL_FD)
echo " parent message => $MESSAGE"  >&2

The exact error message:

read: Illegal option -u

Anyone know what this could possibly be about?

12
  • the file descriptor is definitely defined by NODE_CHANNEL_FD, so that shouldn't be part of the problem Dec 9 '16 at 3:19
  • if I run the bash script with the bash executable, instead of the sh executable, the error goes away. But what confuses me, is that at the top of the of the shell script, is the hashbang - #!/usr/bin/env bash, which I thought meant to tell sh to run the file with bash.... Dec 9 '16 at 3:21
  • Among modern Bourne-like shells, only bash. ksh and zsh support read -u <fd_num>.
    – cuonglm
    Dec 9 '16 at 3:22
  • thanks, yeah, so any idea why /usr/bin/env might not be defined on my system? that's why this is not working AFAICT. The shebang won't work, because /usr/bin/env is not even a directory...I am on Ubuntu 16.04 Dec 9 '16 at 3:24
  • Not sure why /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 9 '16 at 3:25
8

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.

4
  • -u was initially a ksh extension. Also supported by zsh and bash. One can always use read <&"$fd" in standard sh to get the same effect. Sep 4 '17 at 11:27
  • @StéphaneChazelas You're better at shell history than what I am...
    – Kusalananda
    Sep 4 '17 at 12:02
  • 1
    There are very few features that are native to bash. Most for come from ksh and csh, a few from zsh and mksh. If in doubt, you can always check the ksh86 or ksh88 (here ksh88i, but there has been little evolution in ksh88 feature-wise since 1988) man pages. If it's not in ksh88, it may come from ksh93, but then, you'd need to check if it was added in bash or ksh93 first. Sep 4 '17 at 12:59
  • @StéphaneChazelas Thanks! I appreciate the pointers.
    – Kusalananda
    Sep 4 '17 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.