Skip to content
Advertisement

Why does ksh fail to capture standard error during nested command substitutions?

I have the following shell script.

JavaScript

It produces the following output in bash, zsh, dash and posh. It makes sense because there is no such command called bogus on the system.

JavaScript

But in ksh on Debian, it does not print the error message due to invoking the bogus command.

JavaScript

What went wrong?

In case, you want to know about the system details and the version details of the shell, please see the following outputs.

JavaScript

On a CentOS system I see the expected output.

JavaScript

If I remove the command substitution in the function foo, then all shells produce similar output on Debian.

JavaScript

Why doesn’t ksh print the error due to the bogus command in the first example on Debian but produces it on CentOS?

I found the following text in the POSIX.1-2008 standard:

With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results.

I suspect that the part of the text that I have highlighted in bold font is responsible for causing unspecified behaviour in the above examples. However, I am not entirely sure because I couldn’t find a definition for “a script consisting solely of redirections” in the standard.

I have two questions.

  1. Can we prove with sufficient references to the standard or man pages whether the output of ksh in the first example is a bug or not?
  2. How else can I capture the standard error written by a shell function where the shell function is performing command substitution?

Advertisement

Answer

I searched about this issue and found following:

According to standard, this is an error because whenever a command is not found a “command not found” error is given.

This is a bug which has been resolved in beta branch of ksh. Use that branch. I tested it in ubuntu and it works for me.

Here’s how I did that:

JavaScript

There are many bugs in master branch’s ksh. Just check commit logs in https://github.com/att/ast/commits/beta .

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement