Ok, so this is killing me, it might sound like a simple question, but I cannot get it to work Im trying to check a command return code without storing it on a variable and simple cant.
[ $(true) ] && echo Worked
[[ $(true) ]] && echo Worked
[ $(true) -eq 0 ] && echo Worked
[[ $(true) -eq 0 ]] && echo Worked
[ "$(true)" -eq "0" ] && echo Worked
[[ "$(true)" -eq "0" ]] && echo Worked
[ $(true) -eq '0' ] && echo Worked
[[ $(true) -eq '0' ]] && echo Worked
Well, I tried pretty much every single combination, with quotes on one side, both sides, the left side, the right side... Some of them will actually output Worked but they are not actually checking the return code, if you test them with false they will also "Worked"
My final code is to run 5 commands and get the first that works, and the idea is to put them on a if/elseif statement, but I really don't want to put every single command on a variable and then check the variable, its annoying.
true && echo workedgivesworked,false && echo workedgives no echo ...$( )gets its output, not its exit status.[ ]and[[ ]]do tests on strings and numbers, not exit statuses. Things likeifand&&and||test exit statuses directly, so just usecommand && echo Worked. See my answer to a similar question here.cmd1 || cmd2 || cmd3 || cmd4 || cmd5 && echo 'one command succeeded'? This tries each command until one succeeds. If a command succeeds, no further commands in the OR list are executed (it skips to echo). If none succeed, echo doesn't execute.