Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of `//` in Bash parameter expansions?

Tags:

bash

What does //,/ mean in this command?

echo ${foo//,/}
like image 289
Stelios Avatar asked Jan 14 '16 12:01

Stelios


People also ask

What is parameter expansion in bash?

Bash uses the value formed by expanding the rest of parameter as the new parameter ; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter . This is known as indirect expansion .

What is a bash parameter?

In Bash, entities that store values are known as parameters. Their values can be strings or arrays with regular syntax, or they can be integers or associative arrays when special attributes are set with the declare built-in. There are three types of parameters: positional parameters, special parameters, and variables.

What does $# mean in bash script?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

What does $1 mean in bash script?

$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, $0, $1, $3, $4 and so on.


1 Answers

From the bash(1) man page (http://linux.die.net/man/1/bash):

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. 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 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.

That is, ${something//,/} is expanded to the $something with all the occurrences of , removed.

like image 89
Juan Cespedes Avatar answered Sep 30 '22 15:09

Juan Cespedes