diff options
1 files changed, 0 insertions, 5 deletions
| -rw-r--r-- | src/blog/bash-is-weird/index.du | 5 |
diff --git a/src/blog/bash-is-weird/index.du b/src/blog/bash-is-weird/index.du index 8fe78cf..2a33dcc 100644 --- a/src/blog/bash-is-weird/index.du +++ b/src/blog/bash-is-weird/index.du @@ -22,12 +22,10 @@ It has 3 things which really bothered me: 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 Command {b Substitution};https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html}, the text gets executed and it's output {b replaces} the {` $(...)} itself (slightly reminiscent of {url LISP macros;https://lispcookbook.github.io/cl-cookbook/macros.html}). - .p Side note: obviously, the original expected behaviour can happen if we use some quotes: ```bash @@ -37,7 +35,6 @@ myvar=("$(echo A B C)") {` $\{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 there isn't a type at all;https://tldp.org/LDP/abs/html/untyped.html}! Which makes everything even more weird, considering you {i can} {url create variables with a specific type;https://www.gnu.org/software/bash/manual/bash.html#index-declare}. @@ -46,11 +43,9 @@ myvar=("$(echo A B C)") 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}. So all variables are arrays, and by default for integer and string operations, the first value is used? - .p This is strange, but it cannot be the full picture, because {url 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 have the "trace" flag, which only works with functions. So, variables and functions are the same "object"? Unfortunately, I couldn't find any definitive information on the topic, so this train of thought will have to be left unresolved. |