views:

171

answers:

6

The actual situation is a bit complicated, but the issue I'm running into is that I have an echo command within an eval command. Like so:

$ eval echo 'keep   my     spacing'
keep my spacing
$ echo 'keep   my     spacing'
keep   my     spacing

I was wondering how I could keep eval from stripping my spacing so that the first command prints out the same message as the second...


Here's a closer example to what's actually going on:

$ eval `python -c 'print "echo \"keep    my     spacing\""'`
keep my spacing
A: 

eval echo "'keep my spacing'"

Hyman Rosen
Tried that; doesn't work.
+1  A: 
eval "echo 'keep   my     spacing'"
keep   my     spacing

If that does not work for you, please explain more about the actual situation.

Thomas
OK. So I have a Python script that's setting up modifications to the environment...aliases, environment variables, ... That part of it needs to be evaluated with eval and works fine. At the end of that I also want to print out some messages. Before running through eval things are fine and spaced nicely. eval strips all my formatting and makes a long line of text... It does keep newlines though.
+1, that works for me (bash 4.0.33 on kubuntu)
David V.
+1  A: 

It's not the fault of eval:

`python -c 'print "echo \"keep    my     spacing\""'`

prints

"keep my spacing"

Instead you could do this:

python -c 'print "echo \"keep    my     spacing\""' | bash

This prints

keep    my     spacing
tangens
A: 

or consider printf -v

ring bearer
+1  A: 

The problem (in the python example) is that the command substitution (the backquoted expression) isn't protected by quotes. To fix, put double-quotes around it (and to make the quotes nest better, use $() instead of backquotes):

eval "$(python -c 'print "echo \"keep    my     spacing\""')"
Gordon Davisson
A: 
$ . <(python -c 'print "echo \"keep    my     spacing\""')
keep    my     spacing
ephemient