Reputation: 1188
I'm trying to print a value inside a function. But it is not working; it is printing the variable name. Here is my code:
#!/bin/ksh
MyVariable=""
function ValidateVariableValue
{
eval $1="Working!"
echo "$1" #Here is printing the value "MyVariable" instead of "Working!"
}
ValidateVariableValue MyVariable
echo "value is: ${MyVariable}" #Here is printing the correct value that is "Working!"
Do you know how print the value inside the function?
Upvotes: 1
Views: 696
Reputation: 43039
When you call your function, the positional variable $1
is set to MyVariable.
The statement
eval $1="Working!"
is creating a new variable whose name is contained in $1
. In your case, MyVariable
.
So, echo "$1"
is correctly printing the value of $1
which is MyVariable
. You need to use eval
to print the value of the new variable:
eval "echo \$$1"
See this post on Unix & Linux Stack Exchange:
Upvotes: 6