Why does “local” sweep the return code of a command?
问题 This Bash snippet works as I would\'ve expected: $ fun1() { x=$(false); echo \"exit code: $?\"; } $ fun1 exit code: 1 But this one, using local , does not: $ fun2() { local x=$(false); echo \"exit code: $?\"; } $ fun2 exit code: 0 Can anyone explain why does local sweep the return code of the command? 回答1: The reason the code with local returns 0 is because $? "Expands to the exit status of the most recently executed foreground pipeline." Thus $? is returning the success of local You can fix