Exit status wrong with local variable assignment

后端 未结 1 1761
刺人心
刺人心 2021-01-27 01:22

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

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 01:59

    local is a command itself, not just a modifier to an assignment statement. In test1, you are recording the exit status of the local command, not the command in the command substitution. In test2, you've separated the local command from the assignment to the variable marked as local, so $? contains the exit status you are expecting.


    Unrelated, but you don't need to initialize a variable when marking it as local. This works just fine:

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

    temp_file remains unset until you actually assign a value to it, but the name is local once you actually do assign a value.

    0 讨论(0)
提交回复
热议问题