I want to create a bash script or something similar that I can execute in the current shell:

 echo '#!/bin/bash 
  export foo="bar" ' > zoom.sh

but if I want to run this script in the current process:

exec ./zoom.sh

then my terminal will just exit "process completed", even if I use set +e, it still exits.

There's source and eval which will run stuff in the current shell, so maybe eval is my best bet? A bash function will work - but I have to rely on the user to source it in the first place.

  • 2
    You use source or .. eval is for strings. – muru May 31 '19 at 4:55
  • 3
    What are you actually trying to accomplish? Depending on the real goal, any of a number of approaches might be best. – Gordon Davisson May 31 '19 at 5:43
  • just trying to change the current shell - like alter the PATH using export PATH= that kind of thing. – Alexander Mills May 31 '19 at 6:02
  • 1
    @AlexanderMills This is exactly what source is for. – Kusalananda May 31 '19 at 7:02

From manual, looks like exec will not create new process it just replace the current shell, so after execution it will terminate the session too.

exec [-cl] [-a name] [command [arguments]]:
If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non-interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.

$ cat zoom.sh
export foo="bar"
echo $?
$ exec ./zoom.sh
Connection to localhost closed
| improve this answer | |

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