Javier Salas
Javier Salas

Reputation: 1188

How do assign a variable value in a function? unix korn shell

I want to assign a value to a variable (it will be different variables) but I want to use a function to do that, it would be something like "Pass argument by value" as C# do that.

Here is the example what I want to do:

#/bin/ksh
MyVariable=""

ValidateVariableValue()
{
   ${1}="Working with this!"
}

ValidateVariableValue "MyVariable"

echo "value is: ${MyVariable}"

I want that my function receive the name of the variable and assign a value inside of my function.

That is possible? there's any way to do something similar?

Thanks.

Upvotes: 0

Views: 156

Answers (1)

jehutyy
jehutyy

Reputation: 364

You could to like this :

#/bin/ksh
MyVariable=""

function ValidateVariableValue()
{
    eval $1="Working with this!"
}

ValidateVariableValue MyVariable

echo "value is: ${MyVariable}" 

Upvotes: 2

Related Questions