From 8e93cb9b5c97c8111b0efad913ba7cfd128ca191 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Barriat <pierre-yves.barriat@uclouvain.be> Date: Tue, 17 Oct 2023 12:58:56 +0200 Subject: [PATCH] Last changes before session --- Bash.md | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/Bash.md b/Bash.md index 60d1b13..6846b96 100644 --- a/Bash.md +++ b/Bash.md @@ -244,6 +244,34 @@ myvar=$( ls ) --- +## String manipulation + +Consider `string=abcABC123ABCabc` and `filename=myfile.txt` + +* string length : `${#string}` is `15` +* substring extraction : + * `${string:7}` is `23ABCabc` + * `${string:7:3}` is `23A` + * `${string:(-4)}` or `${string: -4}` is `Cabc` +* substring removal from front : + * `${string#a*C}` is `123ABCabc` + * `${filename##myfile}` is `.txt` + +--- + +* substring removal from back : + * `${string%b*c}` is `abcABC123ABCa` + * `${filename%%.txt}` is `myfile` + +## Variable expansion + +* `${variable-default}` : + if `variable` is unset or null, the expansion of `default` is substituted +* `${variable+default}` : + if `variable` is unset or null, nothing is substituted, otherwise the expansion of `default` is substituted + +--- + # Arithmetic | Operator | Operation | @@ -326,7 +354,7 @@ The following operaors can be used beween conditions: # Conditional exemple -(Dont pay too much attention to the details yet) +(Don't pay too much attention to the details yet) ```bash #!/bin/bash @@ -449,6 +477,7 @@ my_array[0]=56.45 my_array[1]=568 echo Number of elements: ${#my_array[@]} # echo array's content +echo ${my_array[2]} echo ${my_array[@]} ``` @@ -466,7 +495,7 @@ echo ${acronyms[ACK]} if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi ``` -The variable expansion `${MYVAR+ABC}` expands to "ABC" is `MYVAR` is set and to nothing otherwise. +> the variable expansion `${MYVAR+ABC}` expands to `ABC` is `MYVAR` is set and to nothing otherwise ```bash declare -A countries=( [ALB]=Albania [BHR]=Bahrain [CMR]=Cameroon [DNK]=Denmark [EGY]=Egypt ) @@ -919,11 +948,11 @@ bash test.sh # Subshells * A subshell is a "child shell" spawned by the main shell ("parent shell") -* A subshell is a separate instance of the command process, run as a new process -* Unlike calling a shell script, subshells inherit the same variables as the original process +* A subshell is a **separate** instance of the command process, run as a new process +* **Unlike calling a shell script** (slide before), subshells inherit the **same** variables as the original process * A subshell allows you to execute commands within a separate shell environment = *Subshell Sandboxing* > useful to set temporary variables or change directories without affecting the parent shell's environment -* Subshells can be used for parallel processing +* Subshells can be used for **parallel processing** --- @@ -981,7 +1010,7 @@ greeting $COUNTRY COUNTRY="France" ./myScript.sh # or bash or exec echo $COUNTRY -greeting $COUNTRY +greeting $COUNTRY # !! ``` ```bash -- GitLab