Why does eval exit the middle of the - && with the help of set -e?

Why bash does what I expect here with a compound command in a subshell:

$ bash -x -c 'set -e; (false && true; echo hi); echo here'
+ set -e
+ false
+ echo hi
hi
+ echo here
here

But DO NOT do what I expect here:

$ bash -x -c 'set -e; (eval "false && true"; echo hi); echo here'
+ set -e
+ eval 'false && true'
++ false

Basically, the difference between β€œeval'-uating a compound command and just executing a compound command. When a shell executes a compound command, non-terminal commands in a compound command that fail do not cause the entire connection command to fail, they just end the command. But when eval launches a compound command, and any nonterminal subcommand completes the command with an error, eval terminates the command with an error.

I think I need to format my eval statement as follows:

eval "false && true" || :

so that the eval command does not exit my subshell with an error, because it works the way I would expect this:

$ bash -x -c 'set -e; (eval "false && true" || :; echo hi); echo here'
+ set -e
+ false
+ echo hi
hi
+ echo here
here

, :

function execute() {
    local command="$1"
    local remote="$2"
    if [ ! -z "$remote" ]; then
        $SSH $remote "$command" || :
    else
        eval "$command" || :
    fi
}

set -e script. ssh - ssh script , , . , , , , . , ssh eval 1, . || : eval ssh, , , eval'd ssh'd .

.

+4
2

, set -e ; . http://mywiki.wooledge.org/BashFAQ/105 . , , , .


.,.

, eval "false && true" false ( ), set -e .

eval "false && true; true", , eval true (). ( , eval set -e, , false && true .)

eval. :

$ bash -x -c 'set -e; (false && true); echo here'
+ set -e
+ false

, , true, :

$SSH $remote "set -e; $command; true"
eval "$command; true"
+2

eval .

eval "false && true" 1, set -e.

0

All Articles