2010-10-27 45 views
1

鉴于下面的字符串变量传递与空格的字符串作为参数传给击功能

VAR="foo   bar" 

我需要它被传递给一个bash功能,并访问它,按照惯例,通过$1。到目前为止,我一直无法弄清楚如何做到这一点:

#!/bin/bash 
function testfn(){ 
    echo "in function: $1" 
} 
VAR="foo   bar" 
echo "desired output is:" 
echo "$(testfn 'foo   bar')" 
echo "Now, what about a version with \$VAR?" 
echo "Note, by the way, that the following doesn't do the right thing:" 
echo $(testfn "foo   bar") #prints: "in function: foo bar" 

回答

2

Bash是聪明和对双引号匹配的内部或一个$(...)结构之外。

因此,echo "$(testfn "foo bar")"是有效的,并且testfn函数的结果将仅被视为echo内部命令的单个参数。

+1

对。我假定不应该使用嵌套的双引号。然而,'echo“(testfn”$ VAR“)”'工作得很好。 – 2010-10-27 08:16:26

相关问题