【问题标题】:Terminal passing arguments from variable containing whitespace终端从包含空格的变量传递参数
【发布时间】:2012-10-16 23:34:39
【问题描述】:

在终端中如何将包含空格的字符串作为参数传递。它实际上跳过了空格之后的部分,只取第一个单词。

$word = 'soccer ball'
shell_exec('casperjs test.js --word='.$word);

那么我怎样才能逃脱它只运行这个命令的空格

casperjs test.js --word=soccer

【问题讨论】:

标签: php shell ubuntu


【解决方案1】:

对于像您描述的情况(shell中空格旁边还有其他特殊字符),PHP具有escapeshellarg函数:

$word    = 'soccer ball';
$command = sprintf('casperjs test.js --word=%s', escapeshellarg($word));
$result  = shell_exec($command);

我注意将 $word 的值保留为 one 参数:

casperjs test.js --word='soccer ball'

参见:

【讨论】:

    【解决方案2】:

    尝试用引号括起来:

    casperjs test.js --word="soccer ball"
    

    【讨论】:

    • 或者在他的情况下shell_exec('casperjs test.js --word="'.$word."');
    • 谢谢大家,但我认为上面有一个错字
    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 2013-01-15
    • 2017-03-23
    • 1970-01-01
    • 2017-12-09
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多