Cleaner single line feed for exiting bash script with error message

I would like to get a test with a cleaner, less ugly single-line layer:

#!/bin/bash test -d "$1" || (echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1) || exit 1 # ... script continues if $1 is directory... 

Basically, I am for something that does not duplicate exit , and, preferably, does not generate a sub-shell (and, as a result, also looks less ugly), but it fits into one line anyway.

+7
bash
source share
1 answer

Without subshell and without duplication exit :

 test -d "$1" || { echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1; } 

You can also refer to the Group Commands :

{}

 { list; } 

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. A semicolon (or new line) requires the following list.

+20
source share

All Articles