linux force command exit status as true
linux force command exit status as true
This saved me once when I had a script that was failing because wget returned a non-zero exit code on a timeout, stopping the whole pipeline. Appending || true forces the exit status to 0 regardless. Use it carefully — you’re telling the shell ‘it’s fine even if it failed’ — so make sure you actually mean that.
- Linux command exit status
Using echo $? to get last executed command exit status
1
2
3
4
$ wget https://www.google.com
$ echo $?
0
- Call a host that does not exists
1
2
3
4
5
6
7
8
$ wget http://www.dummybla.com/
--2020-09-23 19:00:55-- http://www.dummybla.com/
Resolving www.dummybla.com (www.dummybla.com)... failed: nodename nor servname provided, or not known.
wget: unable to resolve host address ‘www.dummybla.com’
$ echo $?
4
- Call a host that does not exists and force wget exit status as true
1
2
3
4
5
6
7
8
$ wget http://www.dummybla.com/ || true
--2020-09-23 19:00:55-- http://www.dummybla.com/
Resolving www.dummybla.com (www.dummybla.com)... failed: nodename nor servname provided, or not known.
wget: unable to resolve host address ‘www.dummybla.com’
$ echo $?
0
This post is licensed under
CC BY 4.0
by the author.