我试图将用户的命令行历史记录中的一个字符串拆分为命令行参数。例如,我想拆分

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 会自动引用变量,因此我的解决方案都没有奏效。

谢谢!

最佳答案

目前,在 fish 中没有干净的方法来做到这一点。

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

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 的命令行缓冲区,它确实正确地标记了其内容。

关于shell - 将字符串拆分为命令行参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40632539/

10-13 03:30