Operating System - HP-UX
1844432 Members
3689 Online
110233 Solutions
New Discussion

How to preserve multiple SPACE in variables -- shell script

 
SOLVED
Go to solution
Gary Yu
Super Advisor

How to preserve multiple SPACE in variables -- shell script

Dear all,

I am writing a shell script, but I had some trouble preserving mulitple SPACE inside a varible, they always got compressed to only one SPACE, e.g

A="aaa"
SPACE=' ' #that's 4 space
B="bbb"
C=${A}${SPACE}${B}
echo $C

for the $C, instead of "aaa bbb", I got "aaa bbb", I don't know why shell compress the spaces?

thanks,
Gary
10 REPLIES 10
Gary Yu
Super Advisor

Re: How to preserve multiple SPACE in variables -- shell script

hmmm, funny, spaces also got compressed by this forum ;)

I mean, for $C, instead of "aaa----bbb" (I'm uing '-' as a place holder for SPACE), it's always "aaa bbb" just like what you saw in my first input above.
Elmar P. Kolkman
Honored Contributor

Re: How to preserve multiple SPACE in variables -- shell script

Nice that the ITRC does the same as your shell ;-)

The solution:
C="${A}${SPACE}${B}"
echo "${C}"

Your problem is caused by the multiple arguments to echo... By enclosing it in double quotes that problem is solved. The quotes in the assignment is just precaution.
Every problem has at least one solution. Only some solutions are harder to find.
A. Clay Stephenson
Acclaimed Contributor

Re: How to preserve multiple SPACE in variables -- shell script

the key is to quote the assignment statement.
C="${A}${SPACE}${B}"

Note: You must use the double quotes because you want the variables to actually be instantiated. The single quotes would simply quote exactly what is within them.
If it ain't broke, I can fix that.
Gary Yu
Super Advisor

Re: How to preserve multiple SPACE in variables -- shell script

thank you guys for the quick reply, but I've already tried the double quote, but still got the same problem... I'm using /usr/bin/shell on HPUX 11.0

thanks,
Gary
Elmar P. Kolkman
Honored Contributor
Solution

Re: How to preserve multiple SPACE in variables -- shell script

Gary,

I did try it and with me it worked. Mind, the quotes for the echo are more important as the quotes for the assignment !!!

But check out which echo you're calling. That could be another reason for your problem. If it's an alias, for instance, or a shell script doing some stuff, it can also destroy things.

To check it, do:
type echo

It should be 'shell builtin'.

Rgds, Elmar
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: How to preserve multiple SPACE in variables -- shell script

Though I couldn't test with /usr/bin/shell. I only have /usr/bin/sh or /usr/bin/ksh.
Every problem has at least one solution. Only some solutions are harder to find.
Gary Yu
Super Advisor

Re: How to preserve multiple SPACE in variables -- shell script

Sorry Elmar and Clay, my mistake here, I should have the quote in echo command also,
not only in the C="${A}${SPACE}${B}" assignment.

OK problem solved, thanks again guys, I should have assigned 10 points to you two, but that can't be modified, I o u one ;)

Gary
Jean-Luc Oudart
Honored Contributor

Re: How to preserve multiple SPACE in variables -- shell script

Gary

Elmar is right.
Protect with echo
echo $C
aaa bbb
echo "$C"
aaa bbb

pipe into od
echo $C | od -x
6161 6120 6262 620a

echo "$C" | od -x
6161 6120 2020 2062 6262 0a00

Regards,
Jean-Luc
fiat lux
A. Clay Stephenson
Acclaimed Contributor

Re: How to preserve multiple SPACE in variables -- shell script

But you are making exactly the same mistake with your echo.

There is a marked difference in the way the shell handles
echo ${C}
and
echo "${C}"
when the instantiated variable contains whitespace.
what really happens if you echo ${C} is that
echo sees two arguments -- your ${A} and ${B} and you really want it to see only one and that is why the quotes are needed.
In the assignment the quotes allow you to also keep leading and trailing whitespace.
If it ain't broke, I can fix that.
Elmar P. Kolkman
Honored Contributor

Re: How to preserve multiple SPACE in variables -- shell script

Well Clay,

For some strange reason the spaces in the assingment work correctly. I tested it by doing:

SPACE=" " #(4 spaces)
B="sjdhgf " #(4 trailing spaces)
C=A${SPACE}${B}
echo "${C}k" #to make sure the trailing spaces are printed

This gives the correct output.
And this isn't really as strange as it looks. The ${SPACE} worked too. If the shell had done something with it, it would have tried to execute bbb in Gary's original script with aaa assigned to C, instead of assigning "aaa bbb" to C. That's also the reasing for the precautionary double quotes I put in the assignment. If someone tried running the script with another shell the result might be otherwise without the double quotes. With them, the result is sure as long as we are not calling scripts or aliases instead of the real commands.
Every problem has at least one solution. Only some solutions are harder to find.