Return values in bash functions are a popular topic, but I have not seen an answer that explains the behavior I'm seeing.
I have a bash function library "pt2026_firmware", including a function "pt2026_start_firmware" that explicitly returns 0 for success, 1 for failure. When I run
bash -x -c "source pt2026_firmware; pt2026_start_firmware; echo $?"
I get:
+ source pt2026_firmware
[...stuff deleted...]
+ '[' 0 -eq 0 ']'
+ return 1
+ echo 0
0
I'm obviously missing something very basic, but I don't understand why I'm getting $?=0 when I "return 1"? Thanks in advance for your help!
If you want $?
to be expanded after pt2026_start_firmware
runs, you need to use single quotes around your code:
bash -x -c 'source pt2026_firmware; pt2026_start_firmware; echo $?'
Otherwise, the echo $?
is changed to echo 0
before your new copy of bash
even starts.