Simon Tatham on Nostr: In bash, writing ${var?} instead of just ${var} or $var means if var isn't defined ...
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 file2
mv file1 file2 ${subdir?} # error message instead of disaster
My 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!
Published at
2024-04-25 16:12:29Event JSON
{
"id": "2793659480bc9d65de2b4c4cbb8d6d2e85c8cddf3fa89d93a55159bd43a56b44",
"pubkey": "19de6bb53e4860106b7a92e57fbec1cef642164c3ebe0c76eb8ab38c2727672f",
"created_at": 1714061549,
"kind": 1,
"tags": [
[
"proxy",
"https://hachyderm.io/users/simontatham/statuses/112332737699741203",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://hachyderm.io/users/simontatham/statuses/112332737699741203",
"pink.momostr"
]
],
"content": "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.\n\nmv file1 file2 $subdir # oops, I overwrote file2\nmv file1 file2 ${subdir?} # error message instead of disaster\n\nMy 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!",
"sig": "30d548dc3952b5e382c1cb6d7f01695a3f3b352350d1251377285b55cba655a108cc1ef8e2683c118a86d37c16df4421934f90aaa8cfb97361603b007c44fbd8"
}