2

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.

1 Answers1

2

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
#!/bin/bash
export foo="bar"
echo $?
$ exec ./zoom.sh
0
Connection to localhost closed
muru
  • 72,889