diff --git a/Bash.md b/Bash.md index 597256bfed9dd882b74239208a2a19d9c2b80949..6a41aa8e1f225f8975307db29676ab5e39ec5fbd 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