具有tmux发送键的BASH功能 [英] BASH function with tmux send-keys

查看:70
本文介绍了具有tmux发送键的BASH功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将发送键"放入bash函数时遇到问题.这是一个最小的示例:

I'm having problems putting "send-keys" into a bash function. Here's a minimal example:

function keys {
  tmux send-keys -t work:1 $*
}

tmux new-session -d -s work
keys "pwd" c-m "ls -latr" c-m
tmux attach-session -t work

这里的keys参数正是我在命令行上键入的作为 tmux send-keys 的参数.它几乎可以工作,但是会删除空格,所以我将 ls-latr 看作一个单词.但是,如果我在函数中的 $ * 周围加上引号,它只会在一行上输出整个keys参数(将 c-m 作为文字字符处理).我如何使其像执行命令行一样执行send-keys参数?

The keys argument here is exactly what I'd type on the command-line as an argument to tmux send-keys. It almost works, but strips spaces, so I see ls-latr all as one word. But if I put quotes around the $* in the function, it just outputs the entire keys argument on one line (treating the c-m as literal characters). How could I make it execute the send-keys argument as if I'd typed it from command-line?

推荐答案

您应该在您的计算机中使用"$ @" (引号很重要),而不要使用 $ * 外壳功能;它将完全保留函数调用中提供的位置参数.

You should use "$@" (the quotes are important) instead of $* in your shell function; it will preserve the positional parameters exactly as they are supplied in the function invocation.

function keys {
  tmux send-keys -t work:1 "$@"
}

使用"$ @" ,最终命令将获得四个原始参数:

With "$@", the final command will get the four original arguments:

tmux send-keys -t work:1 'pwd' 'c-m' 'ls -latr' 'c-m'

代替未引用的 $ * 中的五个:

Instead of the five from unquoted $*:

tmux send-keys -t work:1 pwd c-m ls -latr c-m

或者是"$ *" 中的一个:

tmux send-keys -t work:1 'pwd c-m ls -latr c-m'


在未加引号的情况下, $ * $ @ 实际上是相同的,但是在不加双引号的情况下它们却有很大不同.


When unquoted, $* and $@ are effectively identical, but they are significantly different when unsed in double quotes.

  • $ * $ @ 就像 $ 1 $ 2 $ 3…

产生的值会受到分词和文件名扩展(又称文件名扩展)的影响,因此您通常 不想使用这些(或任何其他参数扩展名)而无需双引号.

The resulting values are subject to word splitting and filename expansion (a.k.a. globbing), so you usually do not want to use these (or any other parameter expansions) without double quotes.

额外的单词拆分是为什么您的"ls -ltr" ( key 的一个参数)变为 ls -ltr ( tmux发送键).

The additional word splitting is why your "ls -ltr" (one argument to key) becomes ls -ltr (two arguments to tmux send-keys).

"$ *" 就像"$ 1 $ 2 $ 3…"

所有位置参数值都被合并为一个单独的单词"(字符串),可以防止进一步的单词拆分和模糊化.

All the positional parameter values are joined into a single "word" (string) that is protected from further word splitting and globbing.

放置在每个位置参数值之间的字符实际上是IFS中的第一个字符;这通常是一个普通的空间.

The character that is put between each positional parameter value is actually the first character from IFS; this is usually a plain space.

"$ @" 就像"$ 1""$ 2""$ 3"…

每个位置参数都被扩展为一个单独的单词,并且可以防止它们进一步分裂和阻塞.

Each positional parameter is expanded into a separate word and they are protected from further word splitting and globbing.

这篇关于具有tmux发送键的BASH功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆