Search code examples
shellunixexecdefects

Shell session exited by itself after issuing command "exec<>5"


When issued two commands below in Linux or MacOS, the Shell session exited and no any error message displayed.

➜  ~  sh
sh-3.2$ exec<>5
sh-3.2$ exit    ==> 'exit' was issued by the shell itself!

Is this a defect of '/bin/sh'? Can anyone provide any insight?

/bin/sh on my RHEL:

-sh-4.1$ sh --version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc.

/bin/sh on my MacOS:

➜ ~ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.


Solution

  • The default file descriptor before a "<" is stdin, so read your command as exec 0<>5. That is, replace stdin with read/write from/to a file named "5". Since the file is empty, the shell exits (no more input.) If you actually put shell commands in the file you can see this happen. Try this:

    echo 'echo here i am; sleep 3' >5
    exec <>5