bash - Assign command output to a variable and do if branch on the return value -


i want following thing

x=$(some_command) if [ $? == 0 ];   do_something   echo $x else   do_something_else fi 

basically, want execute command, store output variable. meanwhile, want branch based on whether command succeeded or not. above way works, looks ugly. smarter way?

thanks.

if x=$(some command);   do_something   echo $x else   do_something_else fi 

the if command works running command , testing whether successful, , executing then or else branch depending on it.


Comments