From 6667de67dfb47aca86d0493f363c46d53b929d8b Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Sun, 5 Mar 2023 08:02:49 +0000 Subject: [PATCH] Autosave: 2023-03-05 08:02:49 --- .../Shell/Data_types_in_Bash.md | 41 +++++++++++++++ Programming_Languages/Shell/Loops_in_bash.md | 12 +++++ .../Shell/Variables_and_data_types.md | 51 ++++--------------- _meta/Topic_Log.md | 9 ---- 4 files changed, 64 insertions(+), 49 deletions(-) create mode 100644 Programming_Languages/Shell/Data_types_in_Bash.md diff --git a/Programming_Languages/Shell/Data_types_in_Bash.md b/Programming_Languages/Shell/Data_types_in_Bash.md new file mode 100644 index 0000000..e855370 --- /dev/null +++ b/Programming_Languages/Shell/Data_types_in_Bash.md @@ -0,0 +1,41 @@ +--- +categories: + - Programming Languages +tags: + - shell + - data-types +--- + +# Data types in Bash + +> There is no typing in Bash + +- Bash variables do not have types thus bash is neither loosely or strictly typed. Anything you apply the identity operator against becomes a character string variable. +- Bash is however able to distinguish numerical strings which is why arithmetic operations and comparisons work. +- Consequently there is no `null` type either. The closest thing is an empty string, i.e. `APPROX_NULL=""` . + +## Declarations + +You can achieve a sort of typing through the `declare` keyword, although bear in mind this is not enforced and you do not have to use it. + +### `-r` : readonly + +```bash +declare -r var1="I'm read only" +``` + +Roughly equivalent to a `const` : if you attempt to change the value of `var1` it will fail with an error message. + +### `i` : integer + +```bash +declare -i var2="43" +``` + +The script will treat all subsequent occurrences of `var2` as an integer + +### `a` : array + +```bash +declare -a anArray +``` diff --git a/Programming_Languages/Shell/Loops_in_bash.md b/Programming_Languages/Shell/Loops_in_bash.md index 26fb7b2..e944bc4 100644 --- a/Programming_Languages/Shell/Loops_in_bash.md +++ b/Programming_Languages/Shell/Loops_in_bash.md @@ -7,9 +7,21 @@ tags: # Loops in bash +## Loop through an array + ```bash for element in "${arr[@]}" do echo "$element" done ``` + +## Traditional for loop with upper and lower increment range + +```bash +for (( i=0; i<=5;i++ )); do + echo $i +done + +# 1 2 3 4 5 +``` diff --git a/Programming_Languages/Shell/Variables_and_data_types.md b/Programming_Languages/Shell/Variables_and_data_types.md index dc370a4..d276571 100644 --- a/Programming_Languages/Shell/Variables_and_data_types.md +++ b/Programming_Languages/Shell/Variables_and_data_types.md @@ -7,22 +7,14 @@ tags: ## Types -> There is no typing in bash! - -- Bash variables do not have types thus bash is neither loosely or strictly typed. Anything you apply the identity operator against becomes a character string variable. -- Bash is however able to distinguish numerical strings which is why arithmetic operations and comparisons work. -- Consequently there is no `null` type either. The closest thing is an empty string, i.e. `APPROX_NULL=""` . - ## Variables -### Variables that hold character strings - -As noted we use the equality symbol to create a variable: +We use the equality symbol to create a variable: ```bash -PRIM_VAR_STR="My first variable" -PRIM_VAR_FLOAT="50.3" -PRIM_VAR_BOOL="true" +stringVar="My first variable" +floatVar="50.3" +boolVar="true" ``` As there is no typing in bash, the names of these variables are purely notional. @@ -30,36 +22,15 @@ As there is no typing in bash, the names of these variables are purely notional. To invoke a variable we use special brackets: ```bash -echo ${PRIM_VAR_STR} # My first variable -echo ${PRIM_VAR_FLOAT} # 50.3 -echo ${PRIM_VAR_BOOL} # true +echo ${stringVar} # My first variable +echo ${floatVar} # 50.3 +echo ${boolVar} # true ``` -- there is no compunction to use capitals for variables but it can be helpful to distinguish custom variables from program variables (see below) -- quotation marks at declaration are also not strictly necessary however they can help avoid bugs. Also serves as a reminder that every type is basically a string at the end of the day +- Quotation marks at declaration are also not strictly necessary however they can help avoid bugs. Also serves as a reminder that every type is basically a string at the end of the day -## Declarations +## Parameter expansion -You can achieve a sort of typing through the `declare` keyword, although bear in mind this is not enforced and you do not have to use it. +// TODO: What is the difference betweeen `$var`, `${var}` and `"${var}"` ? -### `-r` : readonly - -```bash -declare -r var1="I'm read only" -``` - -Roughly equivalent to a `const` : if you attempt to change the value of `var1` it will fail with an error message. - -### `i` : integer - -```bash -declare -i var2="43" -``` - -The script will treat all subsequent occurrences of `var2` as an integer - -### `a` : array - -```bash -declare -a anArray -``` +Still not very clear on this. diff --git a/_meta/Topic_Log.md b/_meta/Topic_Log.md index 9330da0..31c903a 100644 --- a/_meta/Topic_Log.md +++ b/_meta/Topic_Log.md @@ -22,18 +22,9 @@ ## Bash -- Best way to run a command in a script - is it to `echo` it? -- How to handle the return value of a command - - If it returns multiple values, how to isolate and loop through them -- ~~What the weird variable symbols mean like errors and stuff~~ -- ~~Read up properly about `find` and `read`~~ -- `.list` file extension -- Error handling -- ~~Splitting strings~~ - Awk - https://dane-bulat.medium.com/the-awk-programming-language-an-introduction-7035d343cd30 - Why do we have to do `"$var"` instead of `$var` or `${var}` at times -- The `test` program (does it actually use the word 'test' or is this implicit?) and its use of `-z` and and `-e` flags ## Linux