Bash with the extension '/'

Inside a closed read loop, I see this extension of the variable ${line/device name:} . I tried to run the script with my own input file and it just prints the line.

Can you tell me what this extension does?

+5
source share
2 answers

The variable name line ./for line substitution, that is, "device name:" if exists anywhere in $line .

 > line="a device name: some name" > echo ${line/device name:} a some name 

You can also see the substitutions # and % , which mean substitutions at the beginning and end of the line . Also be careful that this substitution / is a bash-specific function (e.g. ash does not support it, % and # seem portable), so you should use #!/bin/bash instead of #!/bin/sh like hashbang at the beginning your script.

+4
source

It returns $line when deleting the device name: substring. On the bash man page:

 ${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. 
+4
source

All Articles