How do you work with variables outsite of the try catch but changing its value inside? #92
-
Hi, I have some function that change some variable inside the try/catch and return 0 or 1. My first problem is when I return 0 or 1 it throws an error. My second problem is: I placed the return 0 or 1 outside of the try/catch, but I change some variable inside the try/catch. And then I cannot see the new value of this variable. Kind Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Please provide some example code. Hard to reason about this without any examples :) |
Beta Was this translation helpful? Give feedback.
-
When I execute I cannot work with the variable after try/catch
|
Beta Was this translation helpful? Give feedback.
-
Unfortunately, a try/catch block creates a subshell. In bash it's currently impossible to affect the parent shell from a subshells, so the only workaround I can think of would be to capture the try/catch stdout in a variable. |
Beta Was this translation helpful? Give feedback.
-
Or you can save result to file and update variable after I personally use: try {
commandToTry > ${HOME}/.result_of_command 2>&1
} catch {
rm -f ${HOME}/.result_of_command
# handle error
}
results=$(<${HOME}/.result_of_command)
rm -f ${HOME}/.result_of_command |
Beta Was this translation helpful? Give feedback.
Unfortunately, a try/catch block creates a subshell. In bash it's currently impossible to affect the parent shell from a subshells, so the only workaround I can think of would be to capture the try/catch stdout in a variable.