Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "?" mean in ${foo:?} in bash?

Tags:

bash

I have some bash scripts which I am going through and I find that the code uses following construct for many variables:

ID1="{ID2:?}"

. ${PATH1:?}/file1

Can someone please help me in understanding what ? does in this?

like image 215
Ima Avatar asked Sep 12 '25 03:09

Ima


1 Answers

In this context, it raises an error if the parameter is unset or null. Usually, you see a custom error message following the ?, but in the absence of one, a generic error message is printed instead.

$ unset id2
$ id1=${id2:?}
bash: id2: parameter null or not set
$ id1=${id2:?nope}
bash: id2: nope
$ id2=9
$ id1=${id2:?}
$ echo $id1
9
like image 129
chepner Avatar answered Sep 13 '25 20:09

chepner