Remove numbers (strip numeric characters) from the string variable

With bash:

$ printf '%s\n' "${VARIABLE//[[:digit:]]/}"
qwerty

[:digit:] can contain other characters than 0 to 9 depend on your locale. If you want only to remove 0 to 9, use C locale instead.


For variety, here's methods using

tr:

VARIABLE=$(printf '%s' "$VARIABLE" | tr -d '0123456789')

sed:

VARIABLE=$(printf '%s' "$VARIABLE" | sed 's/[0-9]//g')

Bash expansion, by far the most terse:

VARIABLE=${VARIABLE//[0-9]/}

and finally Bash expansion again, this time using the [[:digit:]] character class.

VARIABLE=${VARIABLE//[[:digit:]]/} 

Note that (as others have pointed out) [[:digit:]], should cover anything defined as a digit in your locale.


VARIABLE=qwe123rty567
IFS=0123456789
set -f # Disable glob
printf %s $VARIABLE

qwerty

further manipulation is possible.

VARIABLE=qwe123rty567
IFS=0123456789
set -f # Disable glob
set -- $VARIABLE
IFS=;   VARIABLE=$*
printf "replaced $# numbers in \$VARIABLE. RESULT:\t%s\n" "$*"

replaced 6 numbers in $VARIABLE. RESULT:    qwerty

Tags:

String

Shell

Bash