January 02, 2013

A bashism a week: read

Whether for interacting with the caller, for reading the output of some command, or a file descriptor in general, the read shell command can be found in many scripts.

Unless you stick to the POSIX:2001-required "read variable_name", possibly with the -r option, you should expect problems.

  • You must always pass the name of a variable, even if you are going to discard its content.
  • Prompts, time outs, changing the input delimiter, and basically any feature other than just reading a line is not required to be supported.

dash, for instance, supports prompts but nothing else.

Remember, if you rely on any non-standard behaviour or feature make sure you document it and, if feasible, check for it at run-time.

No comments:

Post a Comment