1

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.

user940178
  • 53
  • 6
  • What we have above it's called "diamond operator" and it's usually used to redirect both stdin and stdout to another file descriptor: exec 5<>file. In your example you don't have the file descriptor, only the file (named 5 on this example). But why it exits, I have no idea. – cristi Nov 21 '15 at 11:23
  • Check this explanation: http://www.catonmat.net/blog/bash-one-liners-explained-part-three/ at "11. Open a file both for writing and reading" – cristi Nov 21 '15 at 11:26
  • you are right, 5 at here is a file, but I don't think sh should exit after hitting such a tiny error. – user940178 Nov 21 '15 at 11:40

2 Answers2

1

When referring to a command line such as Linux or Unix, exec is a BOURNE and POSIX shell command that replaces the current shell process with the command specified after exec. This command does not create a new PID. For example, if you were to run exec <command>, the shell would be replaced by that command. When that command is exited, the shell will exit.
You can also have a look at these links.

http://linux.about.com/od/commands/l/blcmdln_exec.htm

What are the uses of the exec command in shell scripts?

Community
  • 1
  • 1
anand mishra
  • 900
  • 1
  • 11
  • 30
1

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
Jonathan Ross
  • 530
  • 3
  • 10