- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to preserve multiple SPACE in variables -- she...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:18 AM
05-14-2004 02:18 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:21 AM
05-14-2004 02:21 AM
Re: How to preserve multiple SPACE in variables -- shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:23 AM
05-14-2004 02:23 AM
Re: How to preserve multiple SPACE in variables -- shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:26 AM
05-14-2004 02:26 AM
Re: How to preserve multiple SPACE in variables -- shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:32 AM
05-14-2004 02:32 AM
Re: How to preserve multiple SPACE in variables -- shell script
thanks,
Gary
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:36 AM
05-14-2004 02:36 AM
SolutionI 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:38 AM
05-14-2004 02:38 AM
Re: How to preserve multiple SPACE in variables -- shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:38 AM
05-14-2004 02:38 AM
Re: How to preserve multiple SPACE in variables -- shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:43 AM
05-14-2004 02:43 AM
Re: How to preserve multiple SPACE in variables -- shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:43 AM
05-14-2004 02:43 AM
Re: How to preserve multiple SPACE in variables -- shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2004 02:57 AM
05-14-2004 02:57 AM
Re: How to preserve multiple SPACE in variables -- shell script
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.