LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I conserve the blanks when I assign to env variable? (https://www.linuxquestions.org/questions/programming-9/how-can-i-conserve-the-blanks-when-i-assign-to-env-variable-649119/)

Juance 06-13-2008 02:18 PM

How can I conserve the blanks when I assign to env variable?
 
Hi All,

I'm working on Compaq Computer Corporation Tru64 UNIX V5.1.

see I need store the result of this command in a env variable:
(please where you see "-" is blanks)

$ sed -n 3p file
0000311CDS-009384---------------0.00------------0.00-----------0.00

this is my result:
$ line=`sed -n 3p file`
$ echo $line
0000311CDS 009384 0.00 0.00 0.00

I need conserve the blanks! but these are removed.

IMPORTANT NOTE!: I found the same frustation making this thread!
if i write consecutive blanks these are automatically removed after post, thats is why I replaced the " " by "-".

Could you please help me??
Thanks in advace!!!

osor 06-13-2008 03:01 PM

Ignore what I said, the problem was in the quoting, not the field separator.

smoked kipper 06-13-2008 03:07 PM

It is probably only removing the blanks when you print (echo), they should still be in the variable. Quoting it should solve the problem.

Code:

echo "$line"
Actually, it's not a bad idea to quote all variables in bash, including assignments, unless you explicitly want to word split (automatic word splitting is stupid behaviour IMO).

theNbomr 06-13-2008 03:11 PM

Two things are at play here. First, if you post your formated text in [CODE] tags, the formatting will be preserved.
Second, the spaces are kept intact when assigned to your variable. If, however, you pass the variable as an argument to a function or as a commandline argument, the shell will expand it into multiple whitespace-delimited arguments. If you want the variable to be used as a single argument, then enclose it in double quotes:
Code:

grep "$line" file
--- rod.

Juance 06-17-2008 08:28 AM

Thanks guys!!!


All times are GMT -5. The time now is 03:11 AM.