bash ls command file issue

ls -l /md01/EL/MarketData/inbound/ststr/INVENTORY* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'

If I run the above from the command line the output to md_email is formatted correctly as

78213497 May 1 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120430.PINESTREET.CSV.done
77904740 May 2 21:35 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120501.PINESTREET.CSV
77904740 May 2 13:05 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120501.PINESTREET.CSV.done
72218242 May 3 19:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120502.PINESTREET.CSV
77839922 May 3 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120502.PINESTREET.CSV.done
echo `ls -l ${indir}/${client}/CONTRACT* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'` >> $md_email

If I execute the above in a redirect to a file using a script the list does not maintain the file position -

78213497 May 1 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120430.STATESTREET.CSV.done 77904740 May 2 21:35 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120501.STATESTREET.CSV 77904740 May 2 13:05 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120501.STATESTREET.CSV.done 72218242 May 3 18:33 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120502.STATESTREET.CSV 77839922 May 3 12:50 /md01/EL/MarketData/inbound/ststr/INVENTORY.20120502.STATESTREET.CSV.done

I can't figure out if it's the file 'md_email' causing the issue or the way I am redirecting the command.

This is one of the more annoying side effects of useless use of backticks, the flattening of all whitespace into simple spaces.

The echo and the backticks are completely unnecessary here fortunately. You can redirect anything that prints to standard output, not just echo.

ls -l /md01/EL/MarketData/inbound/ststr/INVENTORY* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'  >> $md_email

Removing echo and backticks fixed it. I have no idea why I was using echo - totally useless. I read the link provided. Thanks so much.

echo `ls -l ${indir}/${client}/CONTRACT* |tail -5 |awk '{ print $5,$6,$7,$8,$9 }'\r` >> $md_email

You can try this once .. this may work...