From e982d1a6ab5774894af310c080083625a32d0521 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Fri, 3 Feb 2023 14:51:52 +0000 Subject: [PATCH] Autosave: 2023-02-03 14:51:52 --- .../Shell/Expansions_and_substitutions.md | 36 +++++++-- Programming_Languages/Shell/Quote_marks.md | 31 ++++++++ Programming_Languages/Shell/Test.md | 12 +++ .../Shell/Variables_and_data_types.md | 16 ---- .../Shell/Working_with_numbers_in_Bash.md | 75 +++++++++++++++++++ 5 files changed, 147 insertions(+), 23 deletions(-) create mode 100644 Programming_Languages/Shell/Quote_marks.md create mode 100644 Programming_Languages/Shell/Test.md create mode 100644 Programming_Languages/Shell/Working_with_numbers_in_Bash.md diff --git a/Programming_Languages/Shell/Expansions_and_substitutions.md b/Programming_Languages/Shell/Expansions_and_substitutions.md index 98acf5f..0a93047 100644 --- a/Programming_Languages/Shell/Expansions_and_substitutions.md +++ b/Programming_Languages/Shell/Expansions_and_substitutions.md @@ -14,12 +14,12 @@ Below are the main forms of expansion and substitution: | Representation | Name | | -------------- | -------------------- | | `~` . | Tilde expansion | -| `{...}` | Brace expansion | +| `{..}` | Brace expansion | | `${...}` | Parameter expansion | | `$(...)` | Command substitution | | `$((...))` | Arithmetic expansion | -## Brace expansion +## Brace expansion: `{..}` Brace expansion is for changing a smaller part of a greater whole programmatically. This is best understood by looking at examples: @@ -44,7 +44,7 @@ echo {a...c} a b c ``` -We can also set sequences. If we wanted to count to twenty in intervals of two: +We can also set sequences. If we wanted to count to twenty in intervals of two ``` echo {1..20..2} @@ -53,9 +53,9 @@ echo {1..20..2} > Note that we type _two_ dots **not** an elipsis -## Example use case +### Example use case -We might use brace expansion to generate sequential file names, eg. +We might use brace expansion to generate sequential file names using a pre-defined naming scheme, eg. ``` touch file_{01..12}{a..d} @@ -69,6 +69,28 @@ file_02a file_02b ... file_12d - - ``` + +The syntax here basically means: for each of the elements in the first list, run the second list against them. + +## Parameter expansion: `${...}` + +We use most frequently for returning the value of stored [variables](/Programming_Languages/Shell/Variables_and_data_types.md). Techically we do not have to use the braces, we can retrieve with just `$var` however it's better to use them to minimise interpretation fuck-ups which happen a lot. + +When the braces are used, this allows us to transform the values before they are returned such as only returning from the 6th character: `${var:6}`. + +## Command substition: `$(...)` + +Command substitution (circle-brackets) allows us to put the output of one command inside another. Bash runs the bracketed command in a [sub-shell](/Programming_Languages/Shell/Shell_sessions.md) and then returns it to the main user shell. + +For example: + +```bash +echo "The current directory is $(pwd)." +``` + +## Arithemtic expansion: `$((...))` + +We use arithmetic expansion when we want to calculate numerical values + +See [Working with numbers in Bash](/Programming_Languages/Shell/Working_with_numbers_in_Bash.md) for more. diff --git a/Programming_Languages/Shell/Quote_marks.md b/Programming_Languages/Shell/Quote_marks.md new file mode 100644 index 0000000..954c536 --- /dev/null +++ b/Programming_Languages/Shell/Quote_marks.md @@ -0,0 +1,31 @@ +--- +categories: + - Programming Languages +tags: + - shell +--- + +# Quote marks + +## Single-quotes (aka _strong_ quotes) + +Bash will interpret everything in the string as a literal: + +```bash +echo 'The directory is $(pwd)' +# The directory is $(pwd) +``` + +## Double-quotes + +Bash will interpret strings as strings but will interpret expansions and substitutions as executable processes: + +```bash +$pointlessVar='directory' + +echo "The ${pointlessVar}" + +# The directory is /home/thomas +``` + +It is therefore generally best to use double quotes whenever we wish to return mixed values. diff --git a/Programming_Languages/Shell/Test.md b/Programming_Languages/Shell/Test.md new file mode 100644 index 0000000..2c410ec --- /dev/null +++ b/Programming_Languages/Shell/Test.md @@ -0,0 +1,12 @@ +--- +categories: + - Programming Languages +tags: + - shell +--- + +# Test in bash + +`test` is a built-in command that is used to compare values or determine whether something is the case. + +When we run a test the result we get back is a return status of a `0` or a `1` diff --git a/Programming_Languages/Shell/Variables_and_data_types.md b/Programming_Languages/Shell/Variables_and_data_types.md index 3757993..dc370a4 100644 --- a/Programming_Languages/Shell/Variables_and_data_types.md +++ b/Programming_Languages/Shell/Variables_and_data_types.md @@ -38,22 +38,6 @@ echo ${PRIM_VAR_BOOL} # 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 -### Variables that hold references to programs - -We can store a reference to a bash program with slightly different syntax: - -```bash -user="$(whoami)" -``` - -When we want to invoke a program variable we don't need to use brackets: - -```bash -echo $user # thomasbishop -``` - -> Note that when we declare anything in bash (any time `=` is used) we **do not use spaces!** If you do, the variable will not be set. - ## 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. diff --git a/Programming_Languages/Shell/Working_with_numbers_in_Bash.md b/Programming_Languages/Shell/Working_with_numbers_in_Bash.md new file mode 100644 index 0000000..2bb86ba --- /dev/null +++ b/Programming_Languages/Shell/Working_with_numbers_in_Bash.md @@ -0,0 +1,75 @@ +--- +categories: + - Programming Languages +tags: + - shell +--- + +# Working with numbers in Bash + +We distinguish: + +- **arithmetic expansion** `$(( ))` + - returns the result of literal numbers that can then be stored in a variable +- **artihmetic evaluation** `(( ))` + - perform calculations on _existing_ variables + +An example of expansion: + +```bash +a=3 +((a += 3)) +echo $a +# 6 +((a++)) +# 7 +``` + +> Note: we do not use a dollar-sign when referring to variables within arithmetic evaluation, there is no need. If we do, we get an error. This is because we are using an [expansion](/Programming_Languages/Shell/Expansions_and_substitutions.md), therefore the variables are already being interpreted as variables. + +## Declaring variables as integers + +It is good practice to safeguard against Bash treating numbers as strings to declare them as integers in addition to using arithmetic evaluation, e.g: + +```bash +declare -i b=3 +``` + +Whilst this isn't a strict type, it means we can do this: + +```bash +b=$b+4 +echo b +# 7 +``` + +Without getting `3+4` in return + +## No decimals in bash + +Bash does not support decimal calculations natively. This is what you'd get for example: + +```bash +echo $(( 1/3 )) +# 0 (not 0.33) +``` + +So work with decimals you should use `awk` or `bc` ("basic calculations"). + +Example of using `bc`: + +```bash +declare -i c=1 +declare -i d=3 +e=$(echo "scale=3; $c/$d" | bc) +# 0.333 +``` + +## Random numbers + +Generate a pseudo-random number between 1 and 20: + +```bash +echo $(( 1 + RANDOM % 10)) +# 18 +```