0

I'm trying to run a cli command which returns non zero status code if it doesn't runs successfully. however, how can I capture the error code in such a way that when I run

echo $?

It will display 0 as the exit code

Currently when I do this

aws route53domains register-domain --region us-east-1 --cli-input-json file://1-test-example-com.json > /dev/null 2>&1 

the exit code is 254 which that's the command exit code, however ideally I would like it the exit code to be 0.

I need it to display 0 exit code as if its anything else other than this the pipeline fails.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
meWantToLearn
  • 1,691
  • 2
  • 25
  • 45
  • 1
    Just put `exit 0` at the end of your script. – match Jul 27 '21 at 17:18
  • 5
    `aws ... || true` – Jetchisel Jul 27 '21 at 17:20
  • 1
    @Jetchisel correct answer! thank you! – meWantToLearn Jul 27 '21 at 17:24
  • Another option is to add a bang `!` in front of the command, but not sure if that is acceptable, e.g. `! aws ...` because if the command exits with zero `0` ( a success as far as the shell is concern ) the exit code/status will become `1` , see `help test | grep '! '` – Jetchisel Jul 27 '21 at 17:31
  • 1
    @Jetchisel: You can also replace `true` with the builtin command `:`. See `help :`. But `true` is easier to understand. – Cyrus Jul 27 '21 at 17:39
  • @Cyrus, Nice to know that the command that does nothing has a useful exit status :-) – Jetchisel Jul 27 '21 at 17:41
  • @Cyrus `true` is also a builtin command (per [the POSIX standard](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_06)). It's a "regular" built-in, while `:` is a "special" built-in, but I don't think that matters here. – Gordon Davisson Jul 27 '21 at 19:52
  • @GordonDavisson: Thank you. I had missed the fact that `true` also is builtin command. – Cyrus Jul 27 '21 at 20:07

0 Answers0