我需要在另一个变量中使用变量的值.
这就是我试过的..
set cmd_ts "foo bar"
set confCmds {
command1
command2
$cmd_ts
}
puts "confCmds = $confCmds"
Run Code Online (Sandbox Code Playgroud)
但不是得到
confCmds =
command1
command2
foo bar
Run Code Online (Sandbox Code Playgroud)
我正进入(状态:
confCmds =
command1
command2
$cmd_ts
Run Code Online (Sandbox Code Playgroud)
PS我试过以下无济于事
(几乎)只要你使用花括号就什么都行不通.最好的建议是使用list命令:
set confCmds [list command1 command2 $cmd_ts]
Run Code Online (Sandbox Code Playgroud)
我说(差不多)因为你可以使用subst来对confCmds进行变量替换,但这并不是你想要的,而是充满了危险.你想要的是一个单词列表,其中一个或多个可以由变量定义.这正是上述解决方案为您提供的.
如果需要,可以使用反斜杠将命令分布在多行上:
set confCmds [list \
command1 \
command2 \
$cmd_ts \
]
Run Code Online (Sandbox Code Playgroud)
此解决方案假设您想要的是tcl列表.这可能是您想要的,也可能不是,这一切都取决于您如何处理下游的这些数据.
在评论中,您写道您真正想要的是一串换行符分隔项,在这种情况下您可以使用双引号,例如:
set confCmds "
command1
command2
$cmd_ts
"
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个字符串,其中多行由换行符分隔.小心尝试将其视为命令列表(即:不要'foreach foo $ confCmds'),因为它可能会失败,具体取决于$ cmd_ts中的内容.