LOrD_ARaGOrN
LOrD_ARaGOrN

Reputation: 4486

echo working in a very different way

Here PLUGIN=ABC

$ echo "{\"PluginName\": \"${PLUGIN}\""
""PluginName": "ABC
$ echo "{\"PluginName\":${PLUGIN}\",\"Filename\":\"${VAR}\" , \"ErrorString\":"
","Filename":"ABC" , "ErrorString":eployerProps

However if I change above variable PLUGIN to any other string its working.

$ echo "{\"PluginName\":\"${PLUGINS}\",\"Filename\":\"${VAR}\" , \"ErrorString\":"
{"PluginName":"ABC","Filename":"ABC" , "ErrorString":

Not able to understand whats the reason. This is bash 4 however on other server its working fine.

Upvotes: 1

Views: 53

Answers (2)

Socowi
Socowi

Reputation: 27205

I cannot reproduce your problem. This is what my bash 4.4.23(1) prints:

$ PLUGIN=ABC
$ echo "{\"PluginName\": \"${PLUGIN}\""
{"PluginName": "ABC"

However if I change above variable PLUGIN to any other string its working.

Have you noticed that your second command differs from the first one?

echo "{\"PluginName\":${PLUGIN}\",\"Filename\":\"${VAR}\" , \"ErrorString\":"
                      |       |
            different |        \  different
                      |         |
echo "{\"PluginName\":\"${PLUGINS}\",\"Filename\":\"${VAR}\" , \"ErrorString\":"

However, you could make your life a lot easier by using printf:

$ PLUGIN=ABC
$ VAR=XYZ
$ printf '{"PluginName": "%s"\n' "$PLUGIN"
{"PluginName": "ABC"
$ printf '{"PluginName":"%s","Filename":"%s","ErrorString":\n' "$PLUGIN" "$VAR"
{"PluginName":"ABC","Filename":"XYZ","ErrorString":

or even better for a general approach:

$ printf '{'; printf '"%s":"%s",' PluginName "$PLUGIN" Filename "$VAR"
{"PluginName":"ABC","Filename":"XYZ",

Upvotes: 1

melpomene
melpomene

Reputation: 85767

Here PLUGIN=ABC

No, that would not explain the output you're seeing. It's much more likely that PLUGIN=$'ABC\r' (i.e. A B C followed by a carriage return).

Carriage return moves the cursor back to the beginning of the line when printed to a terminal, which is why your output looks so confusing.

Try echo "$PLUGIN" | cat -v or echo "$PLUGIN" | xxd (or any other hex dump tool) to see what's actually in there.

But not able to do on a specific server only.

If PLUGIN is the result of reading a line from a file, then this file is probably in Windows/DOS format on that server (with Carriage Return / Line Feed endings) instead of Unix format (Line Feed only).

Upvotes: 1

Related Questions