0

Motivated by How can I change my command to run without superuser privilleges but with the changed limit value?, in a bash shell, if I run

exec su $LOGNAME && sleep 100

will the sleep command ever be executed?

I think not, because exec su $LOGNAME will replace the shell in the current process with another shell. When I exit the new shell normally (for &&), the process terminates and there is no process to execute the sleep command.

Thanks.

Tim
  • 101,790

2 Answers2

1

It shouldn't. Either the exec succeeds, and the shell is replaced, or the exec fails in which case && doesn't run the following command. exec somcmd || echo fail or exec 2>somefile && echo ok would be different, though.

ilkkachu
  • 138,973
  • Thanks. I was wondering why exec 2>somefile hangs https://unix.stackexchange.com/questions/447311/why-does-exec-2somefile-hang – Tim Jun 01 '18 at 12:39
0

No it will not, check:

ksh93
$ exec /bin/echo bla && sleep 1 && echo foo
bla

You will get back to the previous shell and the exec replaces the ksh93 instance completely.

schily
  • 19,173