I have the following shell script.
$ cat foo.sh foo() { A=$(uname) printf "hello " bogus } echo output: "$(foo 2>&1)"
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.
$ bash foo.sh output: hello foo.sh: line 5: bogus: command not found $ zsh foo.sh output: hello foo:4: command not found: bogus $ dash foo.sh output: hello foo.sh: 5: foo.sh: bogus: not found $ posh foo.sh output: hello foo.sh:8: bogus: not found
But in ksh on Debian, it does not print the error message due to invoking the bogus
command.
$ ksh foo.sh output: hello
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.
$ uname -a Linux debian1 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u2 (2016-01-02) x86_64 GNU/Linux $ cat /etc/debian_version 8.3 $ ksh --version version sh (AT&T Research) 93u+ 2012-08-01 $ dpkg -l bash zsh dash posh ksh | tail -n 5 ii bash 4.3-11+b1 amd64 GNU Bourne Again SHell ii dash 0.5.7-4+b1 amd64 POSIX-compliant shell ii ksh 93u+20120801-1 amd64 Real, AT&T version of the Korn shell ii posh 0.12.3 amd64 Policy-compliant Ordinary SHell ii zsh 5.0.7-5 amd64 shell with lots of features
On a CentOS system I see the expected output.
$ cat /etc/centos-release CentOS release 6.7 (Final) $ ksh --version version sh (AT&T Research) 93u+ 2012-08-01 $ ksh foo.sh output: hello foo.sh[5]: bogus: not found [No such file or directory]
If I remove the command substitution in the function foo
, then all shells produce similar output on Debian.
$ cat bar.sh foo() { printf "hello " bogus } echo output: "$(foo 2>&1)" $ bash bar.sh output: hello bar.sh: line 4: bogus: command not found $ zsh bar.sh output: hello foo:3: command not found: bogus $ dash bar.sh output: hello bar.sh: 4: bar.sh: bogus: not found $ posh bar.sh output: hello bar.sh:7: bogus: not found $ ksh bar.sh output: hello bar.sh[4]: bogus: not found [No such file or directory]
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.
- 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? - 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:
git clone https://github.com/att/ast.git cd ast git checkout beta bin/package make cp arch/linux.i386-64/bin/ksh /usr/local/bin/ksh
There are many bugs in master branch’s ksh. Just check commit logs in https://github.com/att/ast/commits/beta .