[plug] bash, echo and tabs

Russell Steicke russellsteicke at gmail.com
Wed Aug 6 14:51:51 WST 2008


On Wed, Aug 6, 2008 at 12:14 PM, Matt Kemner <zombie at penguincare.com.au> wrote:
> Hi Steve
>
> On Wed, 6 Aug 2008, quoth Steve Boak:
>
>> however if I type "echo `df -h /mnt`", I get
>> Filesystem Size Used Avail Use% Mounted on /dev/hda3 2.0G 125M 1.9G 7% /
>
>> with all the tabs and the CR replaced by spaces.
>
> Instead of using echo (which replaces those whitespace characters as
> you've discovered" try using printf(1) which is designed to format text
> exactly how you want it - there are printf() functions or equivalents in
> most programming languages, and the command line version works much the
> same.

It's not echo which collapses the white space, but the shell.  echo
only sees what the shell gives it.

A simpler solution is to quote the output from df to prevent the shell
from treating it as many separate arguments, and treat it all as one
string that is passed as a single argument to echo:

$ echo `df -h /home`
Filesystem Size Used Avail Use% Mounted on /dev/mapper/b_vg-home_lvol
172G 154G 8.7G 95% /home
$ echo "`df -h /home`"
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/b_vg-home_lvol
                      172G  154G  8.7G  95% /home
$



-- 
Virus found in this message.



More information about the plug mailing list