diff options
| Commit | 6ac430cfaeb1262672af0f83c66b605a500e3924 (patch) | |
|---|---|---|
| Author | 2023-05-01 18:42:25 +0300 | |
| Committer | 2023-05-01 18:42:25 +0300 | |
| Files | ab551b51e17fba61e9dcdfdf8c12addbf3614e92 /src | |
| syndamia.com-6ac430cfaeb1262672af0f83c66b605a500e3924.tar syndamia.com-6ac430cfaeb1262672af0f83c66b605a500e3924.tar.gz syndamia.com-6ac430cfaeb1262672af0f83c66b605a500e3924.zip | ||
| Parent | dd0df95567ee2b0757665fa009e29a52687ccee9 (diff) | |
[blog/biw] Added blogpost for weirdness in bash
3 files changed, 58 insertions, 0 deletions
| -rw-r--r-- | src/blog.md | 3 | ||||
| -rw-r--r-- | src/blog/bash-is-weird/index.md | 52 | ||||
| -rw-r--r-- | src/blog/index.md | 3 |
diff --git a/src/blog.md b/src/blog.md index bd5ba19..d1881bb 100644 --- a/src/blog.md +++ b/src/blog.md @@ -4,6 +4,9 @@ Articles detailing personal experiences or exploration of certain topics. <br> +&&gb [BASH is weird](/blog/bash-is-weird) +: `Shallow dive` `bash` `Linux` `scripting` + &&gb [Sane explanation of compilers: Part 1](/blog/sane-explanation-of-compilers/part-1.html) : `Deep dive` `Compilers` `C` `yacc` `bison` `math` `Trees` `Regex` `Grammar` `Languages` diff --git a/src/blog/bash-is-weird/index.md b/src/blog/bash-is-weird/index.md new file mode 100644 index 0000000..022ef9c --- /dev/null +++ b/src/blog/bash-is-weird/index.md @@ -0,0 +1,52 @@ +# BASH is weird + +For very basic stuff, BASH is decently simple and intuitive, variables are set with the format `name=value` and they're referenced with `$name`[^parameter-expansion]. +However, once you get into it (as I had to for my [Linuxgraphy by Strabo](/talks/linuxgraphy-by-strabo) presentation), you might find there is a lot of weirdness. + +The example I shared with some friends is: + +```bash +myvar=($(echo A B C)) +myvar=1234 +echo $((myvar += 46)) ${myvar[${myvar:0:1}]} +``` + +As someone who started with strictly typed languages, I can find 3 stuff that are really weird with this: + +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. + + 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, my error is that I thought of `$()` as a function call, which takes any number of arguments, "creates" commands, runs them and returns final (stdout) output. + In actuality, this text construction is called a [Command **Substitution**](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html), the output **replaces** the command itself. + It's more like a [LISP macro](https://lispcookbook.github.io/cl-cookbook/macros.html), where you essentially replace some sort of text (macro name + parameters) with another piece of text (code), but you can also add logic which changes the replacement code depending on circumstances (macro parameters). + + Obviously, the original expected behaviour can happen if we use some quotes: + + ```bash + myvar=("$(echo A B C)") + ``` + +2. On the second line, `myvar` *becomes* an integer and on line 3 we increment it with 46, that is totally fine and expected to work. + `${parameter:offset:length}` returns the substring from `parameter` from `offset` with size `length`, but we're giving it a "number", so it should 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). + + 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, [there isn't a type at all](https://tldp.org/LDP/abs/html/untyped.html)! + Which makes everything even more weird, considering you can [create variables with a specific type](https://www.gnu.org/software/bash/manual/bash.html#index-declare). + +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! + + 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? + + This is weird, but it cannot be the full picture, because [names have attributes](https://linux.die.net/Bash-Beginners-Guide/sect_10_01.html), alongside it's type, a variable (name) can be read-only, but also "trace", which only means something for functions. + So, variables and functions are the same "object"? + Unfortunately there isn't too much info out there on the topic, so for now let's stop that thought and just try to ignore it. + +And this is just one example, there are probably even weirder (to the untrained eye) situations. +Yet, the bigger issue is that deeper understanding can only be found in the source code or some sort of unfindable literature, which is pretty sad. + +[^parameter-expansion]: Of course this is just one of the many ways to reference and work with variables. diff --git a/src/blog/index.md b/src/blog/index.md index bd5ba19..d1881bb 100644 --- a/src/blog/index.md +++ b/src/blog/index.md @@ -4,6 +4,9 @@ Articles detailing personal experiences or exploration of certain topics. <br> +&&gb [BASH is weird](/blog/bash-is-weird) +: `Shallow dive` `bash` `Linux` `scripting` + &&gb [Sane explanation of compilers: Part 1](/blog/sane-explanation-of-compilers/part-1.html) : `Deep dive` `Compilers` `C` `yacc` `bison` `math` `Trees` `Regex` `Grammar` `Languages` |