In a bash script, why does
message='123456789'
echo "${message//[0-9]/*}"display *********
but
message='123456789'
echo "${message//./*}"displays 123456789?
All the documentation I've seen says that . matches any character in regex, even in bash, but it's not working for me. How do you match any character in bash?
1 Answer
Where in the documentation does it say that . means any characterin pattern matching? In man bash it says:
Pattern Matching Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally. The special pattern characters have the following meanings: * Matches any string, including the null string. When the globstar shell option is enabled, and is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent s will match only directories and subdirectories. ? Matches any single character.Regular expressions is not the same as shell pattern matching:
.
If you want to replace all characters with another character using${parameter/pattern/string} syntax in Bash you need to use ? like
that:
$ echo "${message//?/*}"
*********You could use . instead of ? in programs that use regular
expressions such as sed:
$ sed 's,.,*,g' <<< "$message"
********* 3