Suppose that I've got the following script called test.sh
:
#! /bin/sh -
printf '%s\n' "${1:?empty or missing argument}"
When run without any command-line arguments it behaves like this:
$ ./test.sh
./test.sh: 2: ./test.sh: 1: empty or missing argument
Question: Is it possible to change the "./test.sh: 2:" part of the error message?
${1:?missing argument}
would not check whether$1
is missing but if it's empty (and the script could be given one empty argument). For missing, you'd need${1?missing argument}
instead. In any case, if you care about the format, you might as well do it the long way like withif [ "$#" -eq 0 ]; then...
(missing) orif [ -z "$1" ]
(empty). – Stéphane Chazelas Aug 21 '17 at 22:02#!
. (For example in the book Classic Shell Scripting chapter 2.4 and here https://unix.stackexchange.com/q/276751/128489.) – Mateusz Piotrowski Aug 21 '17 at 23:02