johannix
johannix

Reputation: 29658

Formatting with echo command

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

Upvotes: 1

Views: 3629

Answers (6)

ephemient
ephemient

Reputation: 204698

$ . <(python -c 'print "echo \"keep    my     spacing\""')
keep    my     spacing

Upvotes: 0

Gordon Davisson
Gordon Davisson

Reputation: 125768

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\""')"

Upvotes: 1

tangens
tangens

Reputation: 39733

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

Upvotes: 1

ring bearer
ring bearer

Reputation: 20783

or consider printf -v

Upvotes: 0

Thomas
Thomas

Reputation: 181745

eval "echo 'keep   my     spacing'"
keep   my     spacing

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

Upvotes: 1

Hyman Rosen
Hyman Rosen

Reputation: 667

eval echo "'keep my spacing'"

Upvotes: 1

Related Questions