diff options
| Commit | 88a5b49611a0a4d1eb47ba86b9123159443ce4cb (patch) | |
|---|---|---|
| Author | 2025-12-23 22:54:20 +0200 | |
| Committer | 2025-12-23 22:54:20 +0200 | |
| Files | 22ce3c36887f506898cc7636a80abf245ae240e7 /src/blog/bash-is-weird | |
| syndamia.com-88a5b49611a0a4d1eb47ba86b9123159443ce4cb.tar syndamia.com-88a5b49611a0a4d1eb47ba86b9123159443ce4cb.tar.gz syndamia.com-88a5b49611a0a4d1eb47ba86b9123159443ce4cb.zip | ||
| Parent | 0be8d4b427a0f0944552769edc9a080dbaf79cee (diff) | |
feat!: Update dunbar after inline brace character swap
1 files changed, 13 insertions, 13 deletions
| -rw-r--r-- | src/blog/bash-is-weird/index.du | 26 |
diff --git a/src/blog/bash-is-weird/index.du b/src/blog/bash-is-weird/index.du index 8bb9632..3fbc2cd 100644 --- a/src/blog/bash-is-weird/index.du +++ b/src/blog/bash-is-weird/index.du @@ -6,7 +6,7 @@ [$pagenav] 'footnote parameter-expansion Of course this is just one of the many ways to reference and work with variables. -For very basic stuff, BASH is decently simple and intuitive, for example variables are set with the format `[name=value]` and they're referenced with `[\$name]` [$parameter-expansion]. +For very basic stuff, BASH is decently simple and intuitive, for example variables are set with the format [`name=value`] and they're referenced with [`\$name`] [$parameter-expansion]. However, that changes once you start diving deeper (as I had to do for my [url /talks/linuxgraphy-by-strabo Linuxgraphy by Strabo] presentation). An example which illustrates my point would be: @@ -18,13 +18,13 @@ echo $((myvar += 46)) ${myvar[\${myvar:0:1}]} It has 3 things which really bothered me: :ordered -1. The output of `[\$(echo A B C)]` is replaced "literally", and then the whole expression is evaluated. - Meaning, `[myvar=($(echo A B C))]` turns into `[myvar=(A B C)]`, which creates an array with those three letters. +1. The output of [`\$(echo A B C)`] is replaced "literally", and then the whole expression is evaluated. + Meaning, [`myvar=($(echo A B C))`] turns into [`myvar=(A B C)`], which creates an array with those three letters. .p - I thought the "returned" value of `[\$(echo A B C)]` would be the whole string "A B C", so the array would have one element, that string. - However, `[\$()]` isn't really a function, which takes arguments, "creates" commands and returns their output. - It's [url https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Command =[Substitution]=], the text gets executed and it's output =[replaces]= the `[\$(...)]` itself (slightly reminiscent of [url https://lispcookbook.github.io/cl-cookbook/macros.html LISP macros]). + I thought the "returned" value of [`\$(echo A B C)`] would be the whole string "A B C", so the array would have one element, that string. + However, [`\$()`] isn't really a function, which takes arguments, "creates" commands and returns their output. + It's [url https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Command [=Substitution=]], the text gets executed and it's output [=replaces=] the [`\$(...)`] itself (slightly reminiscent of [url https://lispcookbook.github.io/cl-cookbook/macros.html LISP macros]). .p Side note: obviously, the original expected behaviour can happen if we use some quotes: @@ -32,21 +32,21 @@ It has 3 things which really bothered me: ```bash myvar=("$(echo A B C)") -2. On the second line, `[myvar]` *[becomes]* an integer and then we do an arithmetic operation on it, nothing wrong here. - `[\${parameter:offset:length}]` returns the substring from `[parameter]` from `[offset]` with size `[length]`, but we're giving it a "number", so it should either throw some sort of error or return something default, like 0. +2. On the second line, [`myvar`] [*becomes*] an integer and then we do an arithmetic operation on it, nothing wrong here. + [`\${parameter:offset:length}`] returns the substring from [`parameter`] from [`offset`] with size [`length`], but we're giving it a "number", so it should either throw some sort of error or return something default, like 0. However it correctly returns the number 1 (and no, that isn't a default, the number is correctly parsed as a string). .p This shows that numbers are actually strings too, and numerical operations are just weird string operations. Contrary to any dynamically-typed language, it's not like the type of a variable is inferred from it's value, [url https://tldp.org/LDP/abs/html/untyped.html there isn't a type at all]! - Which makes everything even more weird, considering you *[can]* [url https://www.gnu.org/software/bash/manual/bash.html#index-declare create variables with a specific type]. + Which makes everything even more weird, considering you [*can*] [url https://www.gnu.org/software/bash/manual/bash.html#index-declare create variables with a specific type]. -3. Finally, the arguably biggest weirdness of it all, is `[\${myvar[1]}]`. - It doesn't matter how it is represented, at first `[myvar]` was the list of values A, B, C and now it's a number, this should return nothing. - Per contra, the value `[B]` is returned instead! +3. Finally, the arguably biggest weirdness of it all, is [`\${myvar[1]}`]. + It doesn't matter how it is represented, at first [`myvar`] was the list of values A, B, C and now it's a number, this should return nothing. + Per contra, the value [`B`] is returned instead! .p - Via trial and error, we find that our number will be returned when the index is 0, and with index 2 we get `[C]`. + Via trial and error, we find that our number will be returned when the index is 0, and with index 2 we get [`C`]. So all variables are arrays, and by default for integer and string operations, the first value is used? .p |