Skip to main content
added 157 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

One thing that could make the and/or command worthwhile is something like:

command && echo "Success! Exit Code: $?" || echo "Failure! Exit Code: $?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
case "$?" in; 
    0) echo "Command exited with: 0"
       <command if good>
       ;;
    1) echo "Command exited with: 1"
        <command if bad>
        ;;
    255) echo "Command exited with: 255"  # for ssh maybe?
         <command if 255>
         ;;
    *) echo "Command exited with: >2"
        <command for other exit code>
        ;;
esac

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
case "$?" in; 
    0) echo "Command exited with: 0"
       <command if good>
       ;;
    1) echo "Command exited with: 1"
        <command if bad>
        ;;
    255) echo "Command exited with: 255"  # for ssh maybe?
         <command if 255>
         ;;
    *) echo "Command exited with: >2"
        <command for other exit code>
        ;;
esac

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

One thing that could make the and/or command worthwhile is something like:

command && echo "Success! Exit Code: $?" || echo "Failure! Exit Code: $?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
case "$?" in; 
    0) echo "Command exited with: 0"
       <command if good>
       ;;
    1) echo "Command exited with: 1"
        <command if bad>
        ;;
    255) echo "Command exited with: 255"  # for ssh maybe?
         <command if 255>
         ;;
    *) echo "Command exited with: >2"
        <command for other exit code>
        ;;
esac
added 77 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
if [[case "$?" = "0" ]];in; then
    0) echo "Command exited with: 0"
       <command if good>
elif [[ "$?" = "1" ]]; then ;;
    1) echo "Command exited with: 1"
        <command if bad>
else        ;;
    255) echo "Command exited with: >1"255"  # for ssh maybe?
         <command if 255>
         ;;
    *) echo "Command exited with: >2"
        <command for other exit code>
fi        ;;
esac

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
if [[ "$?" = "0" ]]; then
    echo "Command exited with: 0"
    <command if good>
elif [[ "$?" = "1" ]]; then
    echo "Command exited with: 1"
    <command if bad>
else
    echo "Command exited with: >1"
    <command for other exit code>
fi

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
case "$?" in; 
    0) echo "Command exited with: 0"
       <command if good>
       ;;
    1) echo "Command exited with: 1"
        <command if bad>
        ;;
    255) echo "Command exited with: 255"  # for ssh maybe?
         <command if 255>
         ;;
    *) echo "Command exited with: >2"
        <command for other exit code>
        ;;
esac
added 443 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
if [[ "$?" = "0" ]]; then
    echo "Command exited with: 0"
    <command if good>
elif [[ "$?" = "1" ]]; then
    echo "Command exited with: 1"
    <command if bad>
else
    echo "Command exited with: >1"
    <command for other exit code>
fi

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

In my testing so far this has worked:

command && echo "$?" || echo "$?"

Just tells it to echo the exit code if it succeeds or if it fails.

As Sato pointed out below this is essentially the same as:

command; echo "$?"

If you need your script to act on the exit code as is Olivier's concern, it is a non issue. Your script could look something like:

command
if [[ "$?" = "0" ]]; then
    echo "Command exited with: 0"
    <command if good>
elif [[ "$?" = "1" ]]; then
    echo "Command exited with: 1"
    <command if bad>
else
    echo "Command exited with: >1"
    <command for other exit code>
fi
added 86 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163
Loading
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163
Loading