[plug] bash, echo and tabs

Lucas van Staden lvs at dedmeet.com
Wed Aug 6 12:32:25 WST 2008


Hi,

You can change the internal field separator (IFS) before you do your echo.
Good practice is to store the current value, and revert IFS back to the
original value when done.

example:
#!/bin/bash

OLDIFS=$IFS
IFS=""
echo "IFS as null"
echo `df -h /mnt`
IFS=$OLDIFS
echo "IFS as normal"
echo `df -h /mnt`


outputs:
IFS as null
Filesystem            Size Used Avail Use% Mounted on
/dev/sda1              71G   20G   48G  30% /
IFS as normal
Filesystem Size Used Avail Use% Mounted on /dev/sda1 71G 20G 48G 30% /
lvanstaden at deepthought:~/sandbox/rev2006$

Regards
Lucas




Steve Boak wrote:
> Hi
> 
> I'm trying to format some output from a bash script, and there's something
> I just can't seem to figure out or find a reference to.
> 
> if I type "df -h /mnt", I get
> Filesystem            Size  Used Avail Use% Mounted on
> /dev/hda3             2.0G  125M  1.9G   7% /
> 
> 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.
> 
> I've tried "echo -e", but that doesn't work as the tabs aren't escaped in
> the "df" output string.
> 
> Just in case you want to know...
> Debian Etch, Linux 2.6.18-5-686
> GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
> 
> Any suggestions?
> 
> Thanks
> 
> Steve
> 



More information about the plug mailing list