Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we OR || any condition with true like "iw dev interface_name del || true" in Bash?

Tags:

linux

bash

shell

Why would any expression in a bash script be ORed with true?

iw dev interface del || true
like image 861
Hidayath Avatar asked Apr 30 '19 13:04

Hidayath


1 Answers

The command probably occurs in the context of a script using set -e, which will cause the script to exit if any command has a non-zero exit status. If you run iw dev interface del and it fails, the script will exit. If you run iw dev interface del || true, though, even if iw fails, then true will run and it is guaranteed to succeed, meaning the exit status of the full command is zero.

Essentially, <command> || true is an idiom that means "Run <command> but ignore its exit status".

like image 102
chepner Avatar answered Oct 01 '22 05:10

chepner