将字符串拆分为命令行参数

Splitting a String into Command Line Arguments

我正在尝试将用户命令行历史记录中的字符串拆分为命令行参数。比如我要拆分

program argument escaped\ argument.txt -o 'another escaped.output'

进入

$v[1]: program
$v[2]: argument
$v[3]: escaped argument.txt
$v[4]: -o
$v[5]: another escaped.output

我已经尝试了所有可能的解决方案,但由于 fish 会自动引用变量,我的 none 个解决方案有效。

谢谢!

这是您需要的地方eval

$ set s "program argument escaped\ argument.txt -o 'another escaped.output'"
$ eval set v $s
$ printf "%s\n" $v
program
argument
escaped argument.txt
-o
another escaped.output

目前,还没有在鱼身上做到这一点的干净方法。

我能想出的最好的办法就是一个黑客:

set s "program argument escaped\ argument.txt -o 'another escaped.output'"
set oldcmd (commandline)
commandline -r -- $s
set v (commandline -o)
commandline -r -- $oldcmd
printf '%s\n' $v

这会滥用 fish 的命令行缓冲区,它会正确标记其内容。