Post AhFo5XIHXe6wQrNvwu by simontatham@hachyderm.io
(DIR) More posts by simontatham@hachyderm.io
(DIR) Post #AhFo5XIHXe6wQrNvwu by simontatham@hachyderm.io
2024-04-25T16:12:29Z
3 likes, 1 repeats
In bash, writing ${var?} instead of just ${var} or $var means if var isn't defined then bash will throw an error and _not_ execute your command, instead of expanding it to "" and carrying on.mv file1 file2 $subdir # oops, I overwrote file2mv file1 file2 ${subdir?} # error message instead of disasterMy favourite use of this is for example commands in documentation, with placeholders for the user to fill in. Then it's OK if a user accidentally copy-pastes it _without_ filling them in!
(DIR) Post #AhH20SNvxTJiHBSdHM by lanodan@queer.hacktivis.me
2024-04-26T07:55:08.848217Z
0 likes, 0 repeats
@simontatham That's part of POSIX btw.And ${parameter:?} allows to also throw an error when it's null rather than only unset, plus ${parameter:?word} would throw word to standard error, which can allow for better errors.(posix draft note, in the special case of ${@?} and${*?}` it's undefined behavior)