Home>
I checked it out, but I can't do it easily.
Suppose you make the following substitution in a Bash script.
function () {
result = $(command)
}
At this time, the result ofcommand
is assigned toresult
, and the exit status of thiscommand
is checked at the same time.
In the article here , you can write it as follows It actually worked well. However, ifresult
is a local variable, it will not work. Why is this so?
function () {
status = 0
result = $(command) || status = $?
echo $status # This is displayed as 128
}
function () {
local status = 0
local result = $(command) || status = $?
echo $status # will always be 0
}
-
Answer # 1
Related articles
Trends
local itself seems to be a command, and when it succeeds in creating a local variable, it returns 0, so it seems to overwrite the exit status.
So, it was necessary to create a local variable with local first and then assign it.