From 16f325a35270930c2799b52b133c5dd71716a43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20de=20Favereau?= <jerome.defavereau@uclouvain.be> Date: Tue, 8 Oct 2024 09:07:54 +0200 Subject: [PATCH] split slide on variable exapnsion in two --- Bash.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Bash.md b/Bash.md index 597256b..6a41aa8 100644 --- a/Bash.md +++ b/Bash.md @@ -246,22 +246,29 @@ myvar=$( ls ) ## String manipulation -Consider `string=abcABC123ABCabc` and `filename=myfile.txt` +Consider `string=abcABC123ABCabc` * 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 : - * `${filename##myfile}` is `.txt` -* substring removal from back : - * `${filename%%.txt}` is `myfile` -<!--## Variable expansion +--- + +## String manipulation + +Consider `filename=/var/log/messages.tar.gz` -* `${variable-default}` : - if `variable` is unset or null, the expansion of `default` is substituted--> +* substring removal from left : + * `${filename##/var}` is `/log/messages.tar.gz` +* substring removal from right : + * `${filename%%.gz}` is `/var/log/messages.tar` + +You can use `*` to match all characters: + +* `${filename%%.*}` is `/var/log/messages` +* `$(filename##*/)` is `messages.tar.gz` --- @@ -269,7 +276,7 @@ Consider `string=abcABC123ABCabc` and `filename=myfile.txt` | Operator | Operation | | ------------ | --------------- | -| `+` `-` `\*` `/` | addition, subtraction, multiply, divide | +| `+` `-` `\*` `/` | addition, subtraction, multiplication, division | | `var++` | increase the variable var by 1 | | `var--` | decrease the variable var by 1 | | `%` | modulus (remainder after division) | @@ -309,7 +316,7 @@ a=$( expr 10 - 3 ) > it is the **preferred method** --> --- - +# Arithmetic ```bash #!/bin/bash @@ -626,7 +633,7 @@ Try a simple example called `test_arg.sh` : echo $1 $2 $4 echo $0 echo $# -echo $* +echo $@ ``` ```bash -- GitLab