Capturing multiple line output into a Bash variable

后端 未结 7 2295
忘掉有多难
忘掉有多难 2020-11-22 03:17

I\'ve got a script \'myscript\' that outputs the following:

abc
def
ghi

in another script, I call:

declare RESULT=$(./myscr         


        
7条回答
  •  时光说笑
    2020-11-22 03:47

    In addition to the answer given by @l0b0 I just had the situation where I needed to both keep any trailing newlines output by the script and check the script's return code. And the problem with l0b0's answer is that the 'echo x' was resetting $? back to zero... so I managed to come up with this very cunning solution:

    RESULTX="$(./myscript; echo x$?)"
    RETURNCODE=${RESULTX##*x}
    RESULT="${RESULTX%x*}"
    

提交回复
热议问题