Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 3 down vote accepted

This should do it:

sed 's/%//'

Pipe your string through it for the best results.

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

sed is one of the easiest ways

sed -i 's/\%//g' fileName
share|improve this answer

Instead of sed, use tr.

tr -d '%'
share|improve this answer
Good point (+1). tr is faster than sed. – Michał Šrajer Oct 3 '11 at 13:59

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 for more information.

share|improve this answer

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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