Skip to content
Extraits de code Groupes Projets

Split slide on variable exapnsion in two

Fusionnées Jérôme de Favereau de Jeneret a demandé de fusionner jdefavereau/learning-bash:master vers master
1 fichier
+ 18
11
Comparer les modifications
  • Côte à côte
  • En ligne
+ 18
11
@@ -246,22 +246,29 @@ myvar=$( ls )
@@ -246,22 +246,29 @@ myvar=$( ls )
## String manipulation
## String manipulation
Consider `string=abcABC123ABCabc` and `filename=myfile.txt`
Consider `string=abcABC123ABCabc`
* string length : `${#string}` is `15`
* string length : `${#string}` is `15`
* substring extraction :
* substring extraction :
* `${string:7}` is `23ABCabc`
* `${string:7}` is `23ABCabc`
* `${string:7:3}` is `23A`
* `${string:7:3}` is `23A`
* `${string:(-4)}` or `${string: -4}` is `Cabc`
* `${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}` :
* substring removal from left :
if `variable` is unset or null, the expansion of `default` is substituted-->
* `${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`
@@ -269,7 +276,7 @@ Consider `string=abcABC123ABCabc` and `filename=myfile.txt`
| Operator | Operation |
| Operator | Operation |
| ------------ | --------------- |
| ------------ | --------------- |
| `+` `-` `\*` `/` | addition, subtraction, multiply, divide |
| `+` `-` `\*` `/` | addition, subtraction, multiplication, division |
| `var++` | increase the variable var by 1 |
| `var++` | increase the variable var by 1 |
| `var--` | decrease the variable var by 1 |
| `var--` | decrease the variable var by 1 |
| `%` | modulus (remainder after division) |
| `%` | modulus (remainder after division) |
@@ -309,7 +316,7 @@ a=$( expr 10 - 3 )
@@ -309,7 +316,7 @@ a=$( expr 10 - 3 )
> it is the **preferred method**
> it is the **preferred method**
-->
-->
---
---
# Arithmetic
```bash
```bash
#!/bin/bash
#!/bin/bash
@@ -626,7 +633,7 @@ Try a simple example called `test_arg.sh` :
@@ -626,7 +633,7 @@ Try a simple example called `test_arg.sh` :
echo $1 $2 $4
echo $1 $2 $4
echo $0
echo $0
echo $#
echo $#
echo $*
echo $@
```
```
```bash
```bash
Chargement en cours