tmux 发送键吞掉空格
概要:“tmux send-keys”从 bash 命令中删除空格,我不明白为什么(或者如何,真的。)
test ()
{
tmux new -s testsession -d
tmux send-keys -t testsession "time tar -I \"zstd -19 -T0\" -cvf ${1}.tar.zst "${@:2}""
tmux attach -t testsession
}
输入
输入1 输入2 输入3 i\ n\ p\ u\ t\ 4
预期(和期望)输出为
时间 tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2" "input3" "in 放 4"
相反,我得到
时间 tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2input3input4"
注意我省略了 ;厘米
或;在发送键末尾按 ENTER
。 (而且我还简化了原始函数,因为其他部分更简单、更有效。)我这样做是为了更准确地理解昨晚花了几个小时试图暴力破解终端上输出的内容。 “正确”语法(无济于事。)
Synopsis: "tmux send-keys" strips the spaces from a bash command and I don't understand why (or how, really.)
test ()
{
tmux new -s testsession -d
tmux send-keys -t testsession "time tar -I \"zstd -19 -T0\" -cvf ${1}.tar.zst "${@:2}""
tmux attach -t testsession
}
with an input of
input1 input2 input3 i\ n\ p\ u\ t\ 4
Expected (and desired) output is
time tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2" "input3" "i n
p u t 4"
Instead I get
time tar -I "zstd -19 -T0" -cvf input1.tar.zst "input2input3input4"
Note I have omitted the ; C-m
or ; ENTER
at the end of the send-keys. (And I've also simplified the original function since other parts are more straightforward and work.) I've done that to get a more precise understanding of what is outputted on the terminal during several hours spent last night trying to brute-force the 'correct' syntax (, to no avail.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我偶然发现了同样的问题,并且发现了为空格添加关键字“Space”的不太好的解决方案。
因此,在您的情况下,我希望以下命令能够工作:
我从本页的 发送密钥说明“密钥列表”部分。
I stumbled over the same problem and I found the not-so-nice solution of adding the keyword, "Space", for the spaces.
So, in your case, I would expect following command to work:
I got this idea from this page's send-keys description "list of Keys" section.
使用
"$*"
而不是"$@"
:"$*"
将参数表示为单个字符串,而不是拆分为数组。使用
"$@"
时,即使用引号包围,单词边界也会保留,因此将"$@"
发送到另一个程序会单独发送每个输入。这使得将输入从包装函数传递到另一个函数变得很容易。$@
和"$@"
之间的区别在于,虽然"$@"
保留参数拆分,但不会进一步拆分。请参阅 shellcheck wiki。我用于测试的脚本
test.sh
:调用
./test.sh foo 'bar baz'
打印到终端:我正在使用
dash,一个 POSIX 兼容的 shell,但使用
bash
运行它会产生相同的结果。Use
"$*"
instead of"$@"
:"$*"
represents the arguments as single string instead of split into an array.When using
"$@"
, the word boundaries are kept even when surrounded by quotes, so sending"$@"
into another program sends each input seperately. This makes it easy to pass input from a wrapper funciton into another function.The difference between
$@
and"$@"
is that while"$@"
preserves argument splits, it will not split it any further. See shellcheck wiki.The script I used for testing,
test.sh
:Calling
./test.sh foo 'bar baz'
prints to the terminal:I am using
dash
, a POSIX compliant shell, but running it withbash
produced the same result.您可以使用 shell 参数扩展:
Tmux send-keys 将带引号的输入作为按键序列读取,因此我们需要带引号的引号来使 tmux 输入引号。
You can quote the arguments correctly using shell parameter expansion:
Tmux send-keys reads the quoted input as a sequence of key strokes, so we need quoted quotes to make tmux type the quotes.