bash - Exit status wrong with local variable assignment -


the example below shows how if temp_file made local part of same line mktemp called exit status retrieved using $? zero, regardless of whether command succeeded or failed (mktemp_xyz used fails). if temp_file made local in advance $? exit status expected.

can explain going on here please?

#!/bin/bash  test_1() {     local temp_file=$(mktemp_xyz -q -t "test.tmp.xxxxxx")     local make_temp_file_ret_val=$?      echo "temp_file: $temp_file"     echo "make_temp_file_ret_val: $make_temp_file_ret_val" }  test_2() {     local temp_file=""     temp_file=$(mktemp_xyz -q -t "test.tmp.xxxxxx")     local make_temp_file_ret_val=$?      echo "temp_file: $temp_file"     echo "make_temp_file_ret_val: $make_temp_file_ret_val" }  test_1 echo "" test_2 

output is:

$ ./test  ./test: line 6: mktemp_xyz: command not found temp_file:  make_temp_file_ret_val: 0  ./test: line 16: mktemp_xyz: command not found temp_file:  make_temp_file_ret_val: 127 

thanks.

local command itself, not modifier assignment statement. in test1, recording exit status of local command, not command in command substitution. in test2, you've separated local command assignment variable marked local, $? contains exit status expecting.


unrelated, don't need initialize variable when marking local. works fine:

local temp_file temp_file=$(mktemp_xyz -q -t "test.tmp.xxxxxx") 

temp_file remains unset until assign value it, name local once assign value.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -