When I read some tutorial today, I came over with the statements like ${file?my.file.txt}
and ${file:?my.file.txt}
, what do ${file?my.file.txt}
and ${file:?my.file.txt}
mean in Bash script?
-
For your future questions, please do a little research before. I've seen you've asked a bit about Bash in particular, but your questions are hardly based on an actual problem and somewhat lack demonstration of research effort.– slhckOct 5, 2014 at 15:41
-
@slhck Ok. In fact, I just wanna know the specific details. Sorry for that if it conflicts with the style here. Thanks for the patient answers, anyhow.– user3872279Oct 5, 2014 at 15:48
1 Answer
From the manual – and it's always good to check that first:
${parameter:?word}
If
parameter
is null or unset, the expansion ofword
(or a message to that effect ifword
is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value ofparameter
is substituted.
So, it's a safeguard against unset variables, where you could show an error. Also,
Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
But of course, the examples you gave are somewhat arbitrary and I wonder why the tutorial would even include them, let alone including them but not explaining what they do.
-
After I make all these things clear, I will write a really high quality doc. Thx! Oct 5, 2014 at 16:00