Bash Shell: Extract Digits From a String

How do I extract digits only from a given string under Bash shell?

You can use the sed, grep and other shell utilities as follows:

grep -o "[0-9]" <<<"input"
grep -o "[0-9]" <<<"$var"
grep -o "[0-9]" <<<"test123"
grep -o "[0-9]" <<<"1Th5is is a test. 5"
output=$(grep -o "[0-9]" <<<"$var")
echo "$output"
grep -oE "[[:digit:]]{1,}" input.file

See our grep command and grep regex tutorial for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Francis Feb 25, 2011 @ 1:15

    Try using ‘tr’, e.g.,
    $ echo ‘asd;lfj29834slkjajfds298124768ald;09290dsfasd098089adfs’ | tr -d [:alpha:] | tr -d [:punct:]

    • Antonio Mendes Mar 30, 2011 @ 12:41

      Or simply doing this:
      echo “ljhdfkldkfs23094823sdklnklsd23984nks8d8d8s” | tr -cd [:digit:]

      • Eric Sebasta Jun 9, 2015 @ 19:18

        tr -cd [:digit:] #both more effective and easier to read +1

  • Cenk Mar 24, 2011 @ 14:33

    Can we use regex in grep? For example, I only want to extract the http links for every line of my file. Is it possible?

    • Philippe Petrinko Apr 30, 2011 @ 12:43

      Yes Cenk. RegEx are included.

      Read The Fantastic Manual, give it a try, and then share your results with us once you are done. Good reading.
      Looking forward to reading from you.

      GREP(1) User Commands GREP(1)

      grep, egrep, fgrep, rgrep – print lines matching a pattern

      grep [OPTIONS] PATTERN [FILE…]
      grep [OPTIONS] [-e PATTERN | -f FILE] [FILE…]

      grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given
      as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

      In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the
      same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is
      provided to allow historical applications that rely on them to run unmodified.

      Generic Program Information
      –help Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.

      -V, –version
      Print the version number of grep to the standard output stream. This version number should be included in all
      bug reports (see below).

      Matcher Selection
      -E, –extended-regexp
      Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

      -F, –fixed-strings
      Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is
      specified by POSIX.)

      -G, –basic-regexp
      Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

      -P, –perl-regexp
      Interpret PATTERN as a Perl regular expression. This is highly experimental and grep -P may warn of
      unimplemented features.

      Matching Control
      -e PATTERN, –regexp=PATTERN
      Use PATTERN as the pattern. This is useful to protect patterns beginning with hyphen-minus (-). (-e is
      specified by POSIX.)

  • Chris F.A. Johnson Apr 30, 2011 @ 11:42

    In bash, you don’t need any external command:

    printf "%s\n" "${var//[![:digit:]]/}"
    • Philippe Petrinko Apr 30, 2011 @ 12:50

      Real cool Chris.
      I knew Brace Expansion tricks with search/replace pattern, but I never read [bash] accepted RegEx as a pattern. (It does not seem to be written into [bash] [man] page.
      Where did you read it first? That would be great to know. Bash-Hackers site ?

  • Philippe Petrinko Apr 30, 2011 @ 12:54

    So I read here
    that bash patterns are not exactly equivalent to RegEx, but accepts POSIX character class, as you good remembered us.
    Thanks a lot, Chris, and thanks Vivek for his Fantastic Site!

    • Chris F.A. Johnson Apr 30, 2011 @ 13:32

      Bash does not use regular expressions (execpt in [[ xxx =~ regex ]]); it uses filename expansion rules, which also accepts character classes.

      I could just as well have used [!0-9] instead of the character class:

      printf "%s\n" "${var//[!0-9]/}"
      • TheBonsai May 26, 2011 @ 5:49

        For digits usually okay, but for everything else it’s not recommended to use an explicit set like [A-Z] instead of [[:upper:]]

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum