Operating System - HP-UX
1747990 Members
5080 Online
108756 Solutions
New Discussion юеВ

Re: need to retain white space in shell script

 
nishant_4
Occasional Advisor

need to retain white space in shell script

Hi,

I don't want to loose "white space" which is suffix of any string but when i am doing echo, i loose those white spaces. Can anyone help me in this regard? Example

#!/bin/perl
string="123 "
echo "$string"

Output :

123 ## white spaces get removed

required output

123 ##which contains spaces as well.

Thank you
10 REPLIES 10
Cody Marcel
Honored Contributor

Re: need to retain white space in shell script

This is a perl script, not a shell script. Can yo ube more specific about your problem? IN this example there is one space in the string "123 ". You get this output correctly. I dont understand what you mean by the # since that is not in the string.

You may also try using print instead of echo, but both should yield the same result and that is the exact string you input, including spaces.
nishant_4
Occasional Advisor

Re: need to retain white space in shell script

sorry for writing perl.....i don't know space after "123" get deleted although i mentioned it.

#!/bin/ksh
string="123 "
echo "$string"

Output is :

123
in above output white-spaces get trimmed automatically.

But Required O/P should be :

123(with white space)

nishant_4
Occasional Advisor

Re: need to retain white space in shell script


white-space also trimmed in this forum......please consider "dot"(.) as white space and then realize my problem.

Suppose "Dot"as white-space

#!/bin/ksh
string="123....................."
echo "$string"

Output is :

123
in above output white-spaces get trimmed automatically.

But Required O/P should be :

123.......................
Reju George
Honored Contributor

Re: need to retain white space in shell script

First you wrote a perl script, then you wrote a korn shell script :-)... but I am still confused as to why this question is posted here.... !! This question would need to go to the HP-UX forum.... In VB you have the Space() function to add spaces to a string....
nishant_4
Occasional Advisor

Re: need to retain white space in shell script

leave shell or perl script behind.......come to unix command.

consider "Dot" as white-space

$echo "123......................."
123
$
(above is the o/p of command)

But i want output with "white-spaces" how can i achieve it?
Cody Marcel
Honored Contributor

Re: need to retain white space in shell script

The whitespace is there, you just cannot see it. If anything it is the terminal that is not displaying the text. Do this...

$ echo '123 1 ' > t.txt

Then open t.txt in an editor like vi. You will see that the trailing spaces are in fact there.
Percy Bell
Trusted Contributor

Re: need to retain white space in shell script

Use print instead

#!/bin/ksh
string="123 "
string2=" 456"
print "$string$string2"
Percy Bell
Trusted Contributor

Re: need to retain white space in shell script

Looking at the examples that work the key is to quote the echo/print line.

If you use echo "$c" your script should work.
Kapil Jha
Honored Contributor

Re: need to retain white space in shell script

From man

`echo' writes each given STRING to standard output, with a space
between each and a newline after the last one.

So does not matter how many white space you give in echo it would be reduced to one white space between characters , and for only one contunoius string...all white space after character would vanish"
This is what echo do.

If you want to have echo do as said above.
echo "$string"
put $string in quotes.

BR,
Kapil+
I am in this small bowl, I wane see the real world......