Bash/Scripting/Variables

Aus SchnallIchNet
Wechseln zu: Navigation, Suche


Pattern match and return

${variable#pattern}

If the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest.

${variable##pattern}

If the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest.

${variable%pattern}

If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest.

${variable%%pattern}

If the pattern matches the end of the variable's value, delete the longest part that matches and return the rest.

${variable/pattern/string}
${variable//pattern/string}

The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern is begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.a

Substitution Operators

${varname:-word}

If varname exists and isn't null, return its value; otherwise return word.

Purpose: Returning a default value if the variable is undefined.

Example:

${count:-0} 

evaluates to 0 if count is undefined.

${varname:=word}

If varname exists and isn't null, return its value; otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way.

Purpose: Setting a variable to a default value if it is undefined.

Example:

${count:=0} 

sets count to 0 if it is undefined.

${varname:?message}

If varname exists and isn't null, return its value; otherwise print varname: followed by message, and abort the current command or script (non-interactive shells only). Omitting message produces the default message parameter null or not set.

Purpose: Catching errors that result from variables being undefined.

Example:

{count:?"undefined!"} 

prints "count: undefined!" and exits if count is undefined.

${varname:+word}

If varname exists and isn't null, return word; otherwise return null.

Purpose: Testing for the existence of a variable.

Example:

${count:+1} 

returns 1 (which could mean "true") if count is defined.

${varname:offset}
${varname:offset:length}

Performs substring expansion.a It returns the substring of $varname starting at offset and up to length characters. The first character in $varname is position 0. If length is omitted, the substring starts at offset and continues to the end of $varname. If offset is less than 0 then the position is taken from the end of $varname. If varname is @, the length is the number of positional parameters starting at parameter offset.

Purpose: Returning parts of a string (substrings or slices).

Example: If count is set to frogfootman, ${count:4} returns footman.

${count:4:4} 

returns foot.

Gueltigkeitsbereich

alle variablen in einem bash-script sind i.d.r. global.
man sollte also local verwenden wenn man in funktionen
variablen mit dem gleichen namen verwendet wie im hauptprogramm
damit man die werte in der globalen variablen nicht ueberschreibt:

#!/bin/bash
#

var1=hello
var2=world

function blah()
{
   local var2="local_var2"
   echo "$var1 $var2";
}

echo "$var1 $var2";
blah;
echo "$var1 $var2";

wird folgende ausgabe produzieren:

hello world
hello local_var2
hello world

wie man sieht ist $var2 in der funktion mit einem anderen wert ueberschrieben
ohne dabei den wert der globalen $var2 zu veraendern.
wuerde man den zusatz 'local' innerhalb der funktion weg lassen,
wuerde das zu folgender ausgabe fuehren:

hello world
hello local_var2
hello local_var2

der wert der globalen variablen $var2 waere ueberschrieben worden und der urspruengliche inhalt verloren...