Autosave: 2023-03-05 08:02:49

This commit is contained in:
thomasabishop 2023-03-05 08:02:49 +00:00
parent 313278df30
commit 6667de67df
4 changed files with 64 additions and 49 deletions

View file

@ -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
```

View file

@ -7,9 +7,21 @@ tags:
# Loops in bash # Loops in bash
## Loop through an array
```bash ```bash
for element in "${arr[@]}" for element in "${arr[@]}"
do do
echo "$element" echo "$element"
done 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
```

View file

@ -7,22 +7,14 @@ tags:
## Types ## 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
### Variables that hold character strings We use the equality symbol to create a variable:
As noted we use the equality symbol to create a variable:
```bash ```bash
PRIM_VAR_STR="My first variable" stringVar="My first variable"
PRIM_VAR_FLOAT="50.3" floatVar="50.3"
PRIM_VAR_BOOL="true" boolVar="true"
``` ```
As there is no typing in bash, the names of these variables are purely notional. 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: To invoke a variable we use special brackets:
```bash ```bash
echo ${PRIM_VAR_STR} # My first variable echo ${stringVar} # My first variable
echo ${PRIM_VAR_FLOAT} # 50.3 echo ${floatVar} # 50.3
echo ${PRIM_VAR_BOOL} # true 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 Still not very clear on this.
```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
```

View file

@ -22,18 +22,9 @@
## Bash ## 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 - Awk
- https://dane-bulat.medium.com/the-awk-programming-language-an-introduction-7035d343cd30 - 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 - 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 ## Linux