[olug] Variable with Spaces

Chris St. Pierre stpierre at NebrWesleyan.edu
Wed Aug 29 23:53:04 UTC 2007


On Wed, 29 Aug 2007, Stan Coleman wrote:

>   I would like to add a few blank spaces to the front of a variable
> and so far I've had no luck. Here is what I have so far;
> variable=`tail /var/some/temp/file/text.txt` If the text file has
> spaces for the first few characters the $variable removes the spaces.
> What I'd really like to do is add the spaces to the file first like
> this; sed 's/^/           /' /var/some/temp/file/text.txt and then
> have the $variable keep the leading spaces.

It's all in the quoting:

#!/bin/sh
var="   test"
echo ${var}   # loses spaces
echo "${var}" # retains spaces

Chris St. Pierre
Unix Systems Administrator
Nebraska Wesleyan University


More information about the OLUG mailing list