4

I have a variable in my shell script which is not getting resolved properly at run time:

Input

#!/bin/sh
SERVER_ERL_ARGS="+K true +A30 +P 1048576 \
     -kernel inet_default_connect_options [{nodelay,true}]"
echo ${SERVER_ERL_ARGS}

Output:

+K true +A30 +P 1048576 -kernel inet_default_connect_options a

Any reason behind this kind of behavior and how can I correct it.

terdon
  • 242,166

1 Answers1

15

You have not quoted the variable expansion in your echo, and you have a file called a in the current directory.

The [{nodelay,true}] acts like a filename globbing pattern that will match any file whose name is any single character within the [...]. In your case it matches the name of the file called a in the current directory.

So, quote the variable, but it would be even better to use printf:

printf '%s\n' "$SERVER_ERL_ARGS"

Also note that ${variable} is exactly equivalent to $variable in all cases except when the expansion is part of a string where the immediately following character is valid in a variable name, like in "${variable}x".

See also:

Kusalananda
  • 333,661
  • But shouldn't the output be a [true]? I would suppose the [{nodelay,true}] to first expand to [nodelay] [true], then the first pattern is replaced by a, but the second will either get replaced by any file called t, r, u or e or remain as [true], if no file did match. Wrong? – Philippos Oct 11 '17 at 13:26
  • 3
    @Philippos Brace expansion happens before variable expansion. When the variable is expanded, the braces in its value won't be expanded. That's the difference between echo [{nodelay,true}] and v='[{nodelay,true}]'; echo $v. – Kusalananda Oct 11 '17 at 13:36
  • With (t)csh and zsh, you also need ${var} in things like $var[text] or $var:text. – Stéphane Chazelas Jul 25 '18 at 07:49