remove particular characters from a variable using bash
There is no need to execute an external program. bash
's string manipulation can handle it (also available in ksh93
(where it comes from), zsh
and recent versions of mksh
, yash
and busybox sh
(at least)):
$ VERSION='2.3.3'
$ echo "${VERSION//.}"
233
(In those shells' manuals you can generally find this in the parameter expansion section.)
By chronological order:
tr/sed
echo "$VERSION" | tr -d .
echo "$VERSION" | sed 's/\.//g'
csh/tcsh
echo $VERSION:as/.//
POSIX shells:
set -f
IFS=.
set -- $VERSION
IFS=
echo "$*"
ksh93/zsh/mksh/bash/yash (and busybox ash
when built with ASH_BASH_COMPAT
)
echo "${VERSION//.}"
zsh
echo $VERSION:gs/./
In addition to the successful answers already exists. Same thing can be achieved with tr
, with the --delete
option.
echo "2.3.3" | tr --delete .
echo "2.3.3" | tr -d . # for MacOS
Which will output: 233