Wednesday, July 27, 2022

[SOLVED] bash: compound expression in test command

Issue

What is the right way in bash to do the following:

[ -x /path/to/file ] || multiple_actions

where multiple_actions include e.g. printing some message and exit 1

I tried to do it as:

[ -x /path/to/file ] || echo Can't execute file; exit 1

It works, however I'm not sure if this the right way to do so? May be there are some potential hidden errors?


Solution

What you tried does not do what you want. It always exits with status 1. Either use an if command:

if ! [ -x ... ]; then this; that; fi

or a {} compound command:

[ -x ... ] || { this; that; }


Answered By - Renaud Pacalet
Answer Checked By - David Marino (WPSolving Volunteer)