Skip to main content
2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/

Wrapping the value of a variable with double quotes is not enough to protect any nested variable names. So what's happening is you're setting $params, but when your program executes, $params is having those variables expanded, so they're gone when you evaluate $params later on.

Example

Say we have this situation, similar to yours.

$ nested_var="nested secret"
$ params="the value is: ${nested_var}"

When we evaluate these variables:

$ echo $nested_var
nested secret
$ echo $params
the value is: nested secret

So we can see that the variable in $params definition has already been expanded, so it's no longer in the form $nested_var, it's now netsted_secret.

Using single quotes would help protect $params definition.

$ params='the value is: $nested_var'
$ echo $params
the value is: $nested_var

But now the question is how do we tell Bash to expand this variable, later on. Here's where you could use the command eval.

$ nested_var="nested secret"
$ eval "echo $params"
the value is: nested secret

Changing $nested_var:

$ nested_var="another nested secret"
$ eval "echo $params"
the value is: another nested secret

The eval command has a bad rep though. So I think I would encourage you to do what you're trying using functions instead.

Alternative method

I would be tempted to create a function that you pass into it parameters, and the function would return constructed $params string back.

Example

$ genparam () { printf -- "-game csgo -usercon +map %s -strictportbind -ip %s -port %s +clientport %s +tv_port %s -maxplayers %s\n" "$1" "$2" "$3" "$4" "$5" "$6";  }

Here's an expanded view of that one-liner:

$ genparam () { 
    printf -- "-game csgo -usercon +map %s -strictportbind -ip %s -port %s +clientport %s +tv_port %s -maxplayers %s\n" \
       "$1" "$2" "$3" "$4" "$5" "$6";    \
}

Now when we call our function genparm() we pass it the arguments that we want it to use like so:

$ genparam $defaultmap $ip $port $clientport $sourcetvport $maxplayers
-game csgo -usercon +map de_dust2 -strictportbind -ip 0.0.0.0 -port 27015 +clientport 27005 +tv_port 27020 -maxplayers 16
slm
  • 379.8k
  • 127
  • 793
  • 897