The echo statement can not be used to format numbers under bash / ksh shell. You need to use printf command to format and print data according to FORMAT.
Syntax
The syntax is:
printf "%'.2f" var
OR
printf "%'.2d" var
OR
printf "Total Rs.%'.2f" var
OR
printf "Total $.%'.2f" var
The ' act as a field and printing modifiers for decimal conversions, the thousands grouping separator is applied to the integer portion of the output according to the current LC_NUMERIC. Run the locale program to check current settings for LC_NUMERIC:
locale
Sample outputs:
LANG=en_IN LANGUAGE=en_IN:en LC_CTYPE="en_IN" LC_NUMERIC="en_IN" LC_TIME="en_IN" LC_COLLATE="en_IN" LC_MONETARY="en_IN" LC_MESSAGES="en_IN" LC_PAPER="en_IN" LC_NAME="en_IN" LC_ADDRESS="en_IN" LC_TELEPHONE="en_IN" LC_MEASUREMENT="en_IN" LC_IDENTIFICATION="en_IN" LC_ALL=
I've setup LC_ALL and LANG as follows in bash. You need to put the following lines in your ~/.bashrc and/or ~/.profile files:
export LC_ALL=en_IN.UTF-8 export LANG=en_IN.UTF-8 export LANGUAGE=en_IN.UTF-8
Examples
Type the following commands:
x="240570.578" printf "%'.2f\n" $x printf "Total $%'.2f\n" $x printf "Total INR.%'.2f\n" $x
Sample outputs:
2,40,570.58 Total $2,40,570.58 Total INR.2,40,570.58
Recommended readings
man printf
man 3 printf
man local
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 3 comments… read them below or add one }
Hi,
Thanks for reminding me power of bash
Hi Vivek,
Yes, [printf] may be one of the less known bash shell builtin command, good to give us a short and quick example.
–P
Say $x
Now, how do you format x?