3

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?

Mateusz Piotrowski
  • 4,623
  • 5
  • 36
  • 70
  • 4
    Note that `${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 with `if [ "$#" -eq 0 ]; then...` (missing) or `if [ -z "$1" ]` (empty). – Stéphane Chazelas Aug 21 '17 at 22:02
  • 1
    @jimmij From what I read it is perfectly ok to put a space after `#!`. (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

1 Answers1

1

The answer to your question is 'no'; using that syntax shall output the script, line number, and variable referenced to standard error as you describe. If you want to alter this, pipe standard error into a process to massage it into your desired format in a script which itself runs the script outputting the error message.

DopeGhoti
  • 73,792
  • 8
  • 97
  • 133