Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No code will run after `exec` in bash script [duplicate]

Sample bash script where I'm testing using variable expansion in command names:

test_command_w_variable_expansion_in_name.sh:

#!/bin/bash

# Gabriel Staples
# 21 Mar. 2020

echo "PATH = \"$PATH\""
# PATH="$HOME/bin:$PATH"
# echo "PATH = \"$PATH\""

# 1st, create a command in ~/bin to test here
mkdir -p ~/bin
echo -e "#!/bin/bash\necho \"This is a test script found in ~/bin.\"" > ~/bin/gs_test_script
chmod +x ~/bin/gs_test_script

# 1)
# command: `echo`
CMD_PREFIX="ec"

${CMD_PREFIX}ho "hey" # works
# exec "${CMD_PREFIX}ho" "hey" # works, but then prevents the code below from running!
# eval "${CMD_PREFIX}ho" "hey" # does NOT work, but also throws no error

# 2)
# command: `gs_test_script` from ~/bin
CMD_PREFIX="gs_test"

~/bin/gs_test_script # works!
gs_test_script # works!
${CMD_PREFIX}_script # works!

Output:

$ ./test_command_w_variable_expansion_in_name.sh
PATH = "/home/gabriel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
hey
This is a test script found in ~/bin.
This is a test script found in ~/bin.
This is a test script found in ~/bin.

Questions:

  1. Now, if I uncomment this line: # exec "${CMD_PREFIX}ho" "hey" # works, but then prevents the code below from running!, the code below it no longer runs, and I get this output instead! Notice I no longer get the 3 printouts of This is a test script found in ~/bin. Why?

    $ ./test_command_w_variable_expansion_in_name.sh
    PATH = "/home/gabriel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
    hey
    hey
    
  2. Also, the eval command just below it doesn't work. If I uncomment out that line I get the exact same erroneous output as posted just above, which still doesn't execute my call to gs_test_script three times like it should. Why?

like image 753
Gabriel Staples Avatar asked Jun 16 '26 19:06

Gabriel Staples


1 Answers

Because the exec command will replace the current bash process with the new command to be executed. It does not return to the calling process. So you should not use exec in your script.

exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.
    
    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
    any redirections take effect in the current shell.
    
    Options:
      -a name   pass NAME as the zeroth argument to COMMAND
      -c    execute COMMAND with an empty environment
      -l    place a dash in the zeroth argument to COMMAND
    
    If the command cannot be executed, a non-interactive shell exits, unless
    the shell option `execfail' is set.
    
    Exit Status:
    Returns success unless COMMAND is not found or a redirection error occurs.
like image 141
Changbin Du Avatar answered Jun 21 '26 03:06

Changbin Du



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!