Parameter Substitution (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Parameter substitution, parameter expansion of shell parameter expansion betekent dat in een Bash-expressie een parameter of variabele wordt vervangen door de inhoud van die variabele, al of niet in combinatie met een bewerking.

Parameter expansion wordt aangegeven met $, vaak in combinatie met accolades: ${}, of nog completer: ${parameter}.

${}

Zoiets simpels als het weergeven van de waarde van een variabele, is al een voorbeeld van parameter substitution:

$ a=12
$ echo $a

12

Door accolades toe te voegen, is het concept van expansie misschien nog duidelijker. Ihb., dat parameter-namen binnen accolades geen $-teken krijgen

$ a=12
$ echo ${i}

12

${parameter:-word}

Gebruik deze syntaxis om parameter te vervangen door literal word als deze eerste leeg is. Zoiets als ifnull(a, b) is SQL:

# De parameter bestaat en heeft een waarde:

$ i1="Hello, world!"
$ echo ${i1:-De parameter is leeg!}

Hello, world!
# De parameter bestaat niet - De literal wordt weergegeven

$ i1="Hello, world!"
$ echo ${i3:-De parameter is leeg!}

De parameter is leeg!

Wil je in plaats van een literal een parameter meegeven? Geen probleem: Daar gaat dit artikel over:

$ i1="Hello, world!"
$ i2="Vervanging"
$ echo ${i3:-${i2}}

Vervanging

Als de parameter wel bestaat, maar leeg is:

$ i1=
$ echo ${i1:-Leeg}

Leeg

${parameter/pattern/string}

Parameter expansion kun je gebruiken voor find-&-replace:

$ i1="Hello, world!"
$ echo ${i1/world/moon}

Hello, moon!

Standaard wordt alleen de eerste occurence van pattern vervangen:

$ i1="Hello, world1, world2 & world3!"
$ echo ${i1/world/moon}

Hello, moon1, world2 & world3!

Dat kun je aanpassen door pattern met een / te laten beginnen:

$ i1="Hello, world1, world2 & world3!"
$ echo ${i1//world/moon}

Hello, moon1, moon2 & moon3!

Meer:

The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the 
longest match of pattern against its value is replaced with string. The match is performed according to 
the rules described below (see Pattern Matching). If pattern begins with ‘/’, all matches of pattern are 
replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match 
at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end 
of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following 
pattern may be omitted. If the nocasematch shell option (see the description of shopt in The Shopt 
Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. If 
parameter is ‘@’ or ‘*’, the substitution operation is applied to each positional parameter in turn, and 
the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the 
substitution operation is applied to each member of the array in turn, and the expansion is the resultant 
list.

Some inspiration:

Zie ook

Bronnen