The example below shows how if temp_file is made local as part of the same line that mktemp is called then the exit status retrieved using $? is always zero, regardless of whether the command succeeded or failed (mktemp_xyz is used so that it always fails). If temp_file is made local in advance then the $? exit status is as expected.
Can someone explain what is 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.