replaceall
Unlike Bourne Again Shell (bash), POSIX shell does not have a native find/replace function for replacing all occurrences of a pattern in a given string with another. Below is an implementation written in native POSIX shell that is:
  • Faster than piping to awk, sed, perl, python, ruby, or other langauge
  • Compatible with bash and all other POSIX-compliant shells
  • Optimized to use ${parameter//pattern[/string]} when bash is detected
1
1 #!/bin/sh
2
2 if [ "$BASH_VERSION" ]; then
3
3 replaceall() # $var_to_get $find $replace [$var_to_set]
4
4 {
5
5 eval ${4:-$1}=\"\${$1//\$2${3:+/\$3}}\"
6
6 }
7
7 else
8
8 # NB: On FreeBSD, sh(1) runs this faster than bash(1) runs the above
9
9 replaceall() # $var_to_get $find $replace [$var_to_set]
10
10 {
11
11 eval local __left= __right=\"\$1\" || return
12
12 while :; do
13
13 case "$__right" in *$2*)
14
14 __left="$__left${__right%%$2*}$3"
15
15 __right="${__right#*$2}"
16
16 continue
17
17 esac
18
18 break
19
19 done
20
20 __left="$__left${__right#*$2}"
21
21 eval ${4:-$1}=\"\$__left\"
22
22 }
23
23 fi
24
24 eval echo \"before=[\$1]\"
25
25 replaceall "[email protected]"
26
26 eval echo \" after=[\${4:-$1}]\"
Copied!
Copy link