Juno on Nostr: After a while trying to understand if either ksh or zsh provided a way to prevent ...
After a while trying to understand if either ksh or zsh provided a way to prevent taking strings and undefined variables as 0 when doing arithmetic evaluation, there seems to be no feature specifically for it, sadly.
Closest is using set -o nounset (ksh) and setopt no_unset (zsh) to prevent undefined variables from evaluating to zero. If a "string" contains only numbers, a dot and whitespace, it will be treated as a number. Also, if it only contains the name of any other variable and whitespace, it evaluates to that.
Not that I expected shell languages to provide accurate arithmetic.
As a bonus though, it was cool learning about ksh's compound variables, force_float option and especially discipline functions.
#ksh #zsh #shell #programming
Published at
2024-04-29 19:28:59Event JSON
{
"id": "1857ebfb5b5b5081318cd4c130686e9dd0c1027c8bce05da739ec0c223817266",
"pubkey": "b72aa16192e3c020c7bae5533e65f1c666851adeef411e5b93ef8d7de0503b8a",
"created_at": 1714418939,
"kind": 1,
"tags": [
[
"t",
"programming"
],
[
"t",
"ksh"
],
[
"t",
"zsh"
],
[
"t",
"shell"
],
[
"proxy",
"https://mastodon.bsd.cafe/users/jutty/statuses/112356159617443334",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://mastodon.bsd.cafe/users/jutty/statuses/112356159617443334",
"pink.momostr"
]
],
"content": "After a while trying to understand if either ksh or zsh provided a way to prevent taking strings and undefined variables as 0 when doing arithmetic evaluation, there seems to be no feature specifically for it, sadly.\n\nClosest is using set -o nounset (ksh) and setopt no_unset (zsh) to prevent undefined variables from evaluating to zero. If a \"string\" contains only numbers, a dot and whitespace, it will be treated as a number. Also, if it only contains the name of any other variable and whitespace, it evaluates to that.\n\nNot that I expected shell languages to provide accurate arithmetic.\n\nAs a bonus though, it was cool learning about ksh's compound variables, force_float option and especially discipline functions.\n\n#ksh #zsh #shell #programming",
"sig": "c8a9a2ca089638fdd46dffcf3c9bb53330e424044177ed7846dc9e5fb042a42af63b10e9e1bc9cbe118663207a67fa4f86271a44e73f29e3ddd945b6ee1cad57"
}