0

When I do,

$ exec 6<&0 0</tmp/lines.txt

bash exits. Why?

Thanks,

Eric J.

ericj
  • 1,957
  • 25
  • 36
  • 1
    @mbratch: There are two uses for exec, see http://stackoverflow.com/questions/18351198/what-is-the-use-of-exec-command-in-the-shell-scripting/18351547#18351547 – cdarke Sep 09 '13 at 15:09
  • 1
    The next question is, what are you *trying* to accomplish? I suspect you want to redirect standard input for all the commands that follow, not the current shell itself. – chepner Sep 09 '13 at 15:32
  • @cdarke thanks a bunch. I was not aware of that. – lurker Sep 09 '13 at 15:41

1 Answers1

3

That makes bash read commands from /tmp/lines.txt redirecting its input in the process. There would no longer be any input to process after all those commands in the file so the shell just exits after it, just like executing a shell script.

If you want to not let bash exit after the commands in /tmp/lines.txt were processed, make sure that you could put back its input like:

exec 6<&0 < <(cat /tmp/lines.txt; echo; echo "exec <&6";)

Which send both inputs of /tmp/lines.txt as commands and also exec <&6 that would put back input from &6 encapsulated by process substition.

And a cleaner approach:

exec 6<&0 < <(cat /tmp/lines.txt; echo; echo "exec <&- <&6 6<&-";)

Or simply:

exec 6<&0 < <(cat /tmp/lines.txt; echo; echo "exec <&6-)
konsolebox
  • 66,700
  • 11
  • 93
  • 101