1

Ubuntu 20.04. Using bash. I made a typo. I can simplify to:

a </

Basically: unknown command with stdin redirected from a directory.

I would expect the result to be:

a: command not found

However with Ubuntu 20.04 (not debian) and bash (not busybox, dash, ksh, tcsh), I get:

Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized

Current thread 0x00007f83aacaa740 (most recent call first):
<no Python frame>

What's going on? Why is python involved?

0
1

Bash implements this functionality via a command_not_found_handle function:

$ declare -f -p command_not_found_handle
command_not_found_handle ()
{
    if [ -x /usr/lib/command-not-found ]; then
        /usr/lib/command-not-found -- "$1";
        return $?;
    else
        if [ -x /usr/share/command-not-found/command-not-found ]; then
            /usr/share/command-not-found/command-not-found -- "$1";
            return $?;
        else
            printf "%s: command not found\n" "$1" 1>&2;
            return 127;
        fi;
    fi
}

As you can see, the function attempts to call /usr/lib/command-not-found - which is a python script:

$ file /usr/lib/command-not-found
/usr/lib/command-not-found: Python script, ASCII text executable

with the function name (in this case a) as argument. However the shell has already redirected standard input from / - so it's effectively calling python3 with the input redirection:

$ python3 </
Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized

Current thread 0x00007f16b0ab0740 (most recent call first):
<no Python frame>

You can see the sequence more clearly if you run your command with set -x:

$ set -x
$ 
$ a </
+ a
+ '[' -x /usr/lib/command-not-found ']'
+ /usr/lib/command-not-found -- a
Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized

Current thread 0x00007feb94250740 (most recent call first):
<no Python frame>
+ return 1
2
  • I'd just worked out the what but not the why. Thanks for the details. – jhnc Mar 15 at 0:53
  • @jhnc tbh I was surprised that bash itself doesn't immediately barf on the </ redirection – steeldriver Mar 15 at 1:01

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.