如何在Unix Korn Shell中打印函数内部的参数值

Jav*_*las 1 unix variables shell ksh function

我正在尝试在函数内打印一个值.但它不起作用; 它正在打印变量名称.这是我的代码:

#!/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!"
Run Code Online (Sandbox Code Playgroud)

你知道怎么打印函数里面的值吗?

cod*_*ter 6

调用函数时,位置变量$1设置为MyVariable.

该声明

eval $1="Working!"
Run Code Online (Sandbox Code Playgroud)

正在创建一个名称包含在其中的新变量$1.在你的情况下,MyVariable.

所以,echo "$1"正确打印其值$1MyVariable.您需要使用eval来打印新变量的值:

eval "echo \$$1"
Run Code Online (Sandbox Code Playgroud)

在Unix和Linux Stack Exchange上看到这篇文章: