LDP/LDP/guide/docbook/abs-guide/var-match.sh

35 lines
1.1 KiB
Bash
Raw Normal View History

2001-09-04 13:36:09 +00:00
#!/bin/bash
2005-05-08 20:09:31 +00:00
# var-match.sh:
# Demo of pattern replacement at prefix / suffix of string.
2001-09-04 13:36:09 +00:00
v0=abc1234zip1234abc # Original variable.
echo "v0 = $v0" # abc1234zip1234abc
echo
# Match at prefix (beginning) of string.
v1=${v0/#abc/ABCDEF} # abc1234zip1234abc
# |-|
2004-10-03 17:40:48 +00:00
echo "v1 = $v1" # ABCDEF1234zip1234abc
# |----|
2001-09-04 13:36:09 +00:00
# Match at suffix (end) of string.
v2=${v0/%abc/ABCDEF} # abc1234zip123abc
# |-|
echo "v2 = $v2" # abc1234zip1234ABCDEF
# |----|
echo
# ----------------------------------------------------
# Must match at beginning / end of string,
#+ otherwise no replacement results.
# ----------------------------------------------------
v3=${v0/#123/000} # Matches, but not at beginning.
echo "v3 = $v3" # abc1234zip1234abc
# NO REPLACEMENT.
v4=${v0/%123/000} # Matches, but not at end.
echo "v4 = $v4" # abc1234zip1234abc
# NO REPLACEMENT.
exit 0