I am trying to use eval command to evaluate a variable(HAPROXY_LISTENER_rabbitmq_project-test-BRHM_PORT) which consists of '-' but unfortunately the eval command is unable to interpret the value of variable and trims the variable name after '-' and produces the string output rather than the actual value of the variable.Is it kind a restriction in eval command?What other options do we have?
is not a valid character for a variable name. If you include it by forcing it via ${variable-name}, it will take that to mean "default to the value of 'name' when 'variable' is undefined".
There are probably much, much, much better ways to accomplish your goal than 80-character variable names and mucking with eval. Can you please tell me your actual goal? And I don't mean "dynamic variable names", I mean the actual problem you are trying to solve.
The hyphen can be used in CoBOL code, but it is considered as plain text for Unix shells. I've fallen across this the other way around when I wanted to set a variable to a value built from another variable like this:-
I wanted to end up with myfile_20131206 as the filename format and I got very confused because it always evaluated to just 20131206 (or whatever the date was)
This was because the underscore is considered part of the variable name and the variable fileheader_ was undefined. This is not pretty, but I had to change the code to read:-
I too would like to know what you are actually trying to achieve. We might have a better way to do it, save you some hassle and provide something for others to read and learn from. Indeed, I'm quite happy to learn myself if there is a better way than I am currently using.
Hi Robin/Corona688,
In your example its an underscore which works well but in my case the dynamic variable has a value which consist of a dash(-)
Actually am using eval to evaluate dynamic variables and one of the variable has the value with "-"(Dash).
OK, but this would only make sense if "search-engine" would be a variable with some content. This is not the case (and can't be, see above), so i still do not understand how this eval-construct came to pass.
Could you please explain:
where your values come from (say: a file, a network stream, whatever)
what you want to accomplish (in terms of goals, not techniques)
I am sure we could suggest a better method to accomplish what you want to do then what you came up with right now.
The value of HAPROXY_LISTENER_NAME is "search-engine" and just search-engine doesnt makes any sense it gets combined with HAPROXY_LISTENER
& PORT to form a variable HAPROXY_LISTENER_search-engine_PORT.This variable is exported and has a value so that it could be fetched.
is used to export variables, but the above command returns the following diagnostic:
export: HAPROXY_LISTENER_search-engine_PORT: is not an identifier
In the shell command language, in the C programming language, in the FORTRAN programming language, in the awk programming language, and in hundreds of other languages, "-" is not a valid character in a variable name.
If you are using a language where HAPROXY_LISTENER_search-engine_PORT is a valid variable name and you want to write a shell script to create names like this to be included in source code for that language, we can write shell scripts to do that. We cannot write a shell script that can use HAPROXY_LISTENER_search-engine_PORT as a shell variable name.