The following command will resolve google ip
> ip=`dig +short google.com`
> echo $ip
> 216.58.210.238
Sometimes (especially when internet connection is lost) this command fail with this error
> ;; connection timed out; no servers could be reached
When the command fail and I use $#
the output is 0 for the assigment
> ip=`dig +short google.com`
> echo $#
> 0
> echo $ip # Command failed
> ;; connection timed out; no servers could be reached
How can I save the output of command in variable, and also check if the command succeeded?