6

The "colon sign" and the "dollar question mark" can be combined to check if a sh script does have an argument and assigns it directly to a variable of interest:

cmd=${1:? "Usage: $0 {build|upload|log}"} 

Can someone explain step for step how this works, and where I can find the details on its implementation? For example I would like to be able to call a function instead of printing to stderr.

help() { echo $"Usage: $0 {build|upload|log}" exit 1 } cmd=${1:? help} 

Why is this not possible?

2
  • Perhaps not necessary, but just to make sure. I can just use test $1 && cmd=$1 || help which would work. My question is really about how these built-in operators combine together in the way they do, not the specific result which can obtained in many ways. Commented Aug 15, 2014 at 11:56
  • You might get some utility out of this Commented Aug 15, 2014 at 15:21

1 Answer 1

7

This is covered in Variable expansion:

${parameter:?word} If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. 

Therefore if there is no argument with the execution of the script, then the variable cmd becomes help and is written to standard out and back to the shell.

However, it is just read in and not executed. If you surround it with backticks then it will be executed and run the Usage funtion:

help() { echo "Usage: $0 {build|upload|log}" } cmd=${1:? `help`} 

Unfortunately, this will still present stderr, as the variable expansion is designed to.

You could alternatively, do the following:

cmd=${1:-help} $cmd 
2
  • 1
    Thanks for the reference to "variable expansion". I couldn't find the right term to search for this. The following is for just sh: pubs.opengroup.org/onlinepubs/009695399/utilities/… Commented Aug 15, 2014 at 14:17
  • 3
    No problem, ":?" is not the easiest of google searches! Commented Aug 15, 2014 at 14:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.