I have a bash script where I get the disk usage, for example 60%. How can I remove the % symbol? Using grep or awk?

5 Answers 5


This should do it:

sed 's/%//'

Pipe your string through it for the best results.

  • 2
    There's a "g" missing because otherwise it would only match the first % in each line. So make it 's/%//g'.
    – joechip
    Aug 23, 2011 at 17:14
  • 1
    @joechip: If there's only one %, as per the question, you don't need g.
    – womble
    Aug 23, 2011 at 22:10

There is no need for external tools like tror even sed as bash can do it on its own since forever.


This statement removes the shortest matching substring (in this case an escaped %) from the end of the variable. There are other string manipulating facilities built into bash. It even supports regular expressions. Generally, most of the stuff you normally see people using tr, sed, awk or grep for can be done using a bash builtin. It's just almost noody knows about that and brings the big guns...

See http://tldp.org/LDP/abs/html/parameter-substitution.html#PSOREX1 for more information.


Instead of sed, use tr.

tr -d '%'
  • Good point (+1). tr is faster than sed. Oct 3, 2011 at 13:59

sed is one of the easiest ways

sed -i 's/\%//g' fileName

If the disk usage is in a variable, bash can do the removal as part of a variable substitution:

echo "disk usage: ${diskusagepct%\%} percent"  # prints disk usage: 60 percent
diskusagenum="${diskusagepct%\%}"  # sets diskusagenum to 60

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.