1
$ cat somefile 
sleep 100
$ exec < somefile 
$ sleep 100

Does exec < somefile behave the same as source somefile except that its invoking process terminates upon its completion?

Thanks.

2

The effect is similar, but the behaviour isn’t strictly the same.

exec < somefile connects standard input to somefile, so the shell starts reading from that file instead of the terminal it’s currently connected to. (Well, at least bash does, Zsh behaves differently.) Effectively this results in the current shell processing the contents of somefile, as it would with source somefile. Once it has exhausted somefile, the shell exits.

  • Thanks. Effectively, does the shell always treat somefile in exec < somefile as a shell script? – Tim Jun 1 '18 at 13:14
  • In particular, an interactive Bash still prints the prompt when reading from the file if you do exec < file. And a noninteractive shell might not read commands from stdin at all, so the redirection really only applies to any command the shell starts. – ilkkachu Jun 1 '18 at 13:16
  • Yes, in the same way as running bash somefile (see the “ARGUMENTS” section in the manpage). – Stephen Kitt Jun 1 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.