0

When I do,

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

bash exits. Why?

Thanks,

Eric J.

ericj
  • 2,138
  • 27
  • 44
  • 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
  • 72,135
  • 12
  • 99
  • 105
  • +1 for the process substitution trick. Perhaps make it `exec <&6-` to tidy up fd 6? – je4d Sep 09 '13 at 15:02
  • @je4d Made an update. I actually tried to avoid it at first to make it a bit clear for the user. – konsolebox Sep 09 '13 at 15:07
  • 2
    You don't need to close fd 6 separately/explicitly like that, `<&6-` moves the fd in one step (see bash(1)/"Moving File Descriptors") – je4d Sep 09 '13 at 15:24