简体   繁体   中英

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM