Bash:在新终端中执行带有参数的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43239264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: Execute command WITH ARGUMENTS in new terminal
提问by IDontKnow
So i want to open a new terminal in bash and execute a command with arguments.
As long as I only take something like ls
as command it works fine, but when I take something like route -n
, so a command with arguments, it doesnt work.
The code:
所以我想在 bash 中打开一个新终端并执行一个带参数的命令。只要我只采用类似ls
命令的东西,它就可以正常工作,但是当我采用类似route -n
, 所以带有参数的命令时,它就不起作用了。编码:
gnome-terminal --window-with-profile=Bash -e whoami
#WORKS
gnome-terminal --window-with-profile=Bash -e whoami
#作品
gnome-terminal --window-with-profile=Bash -e route -n
#DOESNT WORK
gnome-terminal --window-with-profile=Bash -e route -n
#不工作
I already tried putting "" around the command and all that but it still doesnt work
我已经尝试将 "" 放在命令周围,但它仍然不起作用
采纳答案by Dario
Try this:
尝试这个:
gnome-terminal --window-with-profile=Bash -e 'bash -c "route -n; read"'
The final read
prevents the window from closing after execution of the previous commands. It will close when you press a key.
finalread
防止窗口在执行前面的命令后关闭。当你按下一个键时它会关闭。
If you want to experience headaches, you can try with more quote nesting:
如果你想体验头痛,你可以尝试更多的引用嵌套:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c "route -n; read -p '"'Press a key...'"'"'
(In the following examples there is no final read
. Let's suppose we fixed that in the profile.)
(在以下示例中没有 final read
。假设我们在配置文件中修复了它。)
If you want to print an empty line and enjoy multi-level escaping too:
如果你想打印一个空行并享受多级转义:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c "printf \\n; route -n"'
The same, with another quoting style:
同样的,还有另一种引用风格:
gnome-terminal --window-with-profile=Bash \
-e 'bash -c '\''printf "\n"; route -n'\'
Variables are expanded in double quotes, not single quotes, so if you want them expanded you need to ensure that the outermost quotes are double:
变量在双引号中展开,而不是单引号,因此如果您希望它们展开,您需要确保最外面的引号是双引号:
command='printf "\n"; route -n'
gnome-terminal --window-with-profile=Bash \
-e "bash -c '$command'"
Quoting can become really complex. When you need something more advanced that a simple couple of commands, it is advisable to write an independent shell script with all the readable, parametrized code you need, save it somewhere, say /home/user/bin/mycommand
, and then invoke it simply as
引用会变得非常复杂。当您需要比简单的几个命令更高级的东西时,建议使用您需要的所有可读的参数化代码编写一个独立的 shell 脚本,将其保存在某处,例如/home/user/bin/mycommand
,然后简单地调用它
gnome-terminal --window-with-profile=Bash -e /home/user/bin/mycommand
回答by Adrian Bartyczak
To start a new terminal and run an initial command in it with arguments, use the following (note: “-e” is now deprecated; “-- ” is used to start the command instead):
要启动一个新终端并在其中运行带有参数的初始命令,请使用以下命令(注意:“-e”现在已弃用;“--”用于启动命令):
gnome-terminal --window-with-profile=Bash -- \
bash -c "<command>"
To continue the terminal with the normal bash profile, add exec bash
:
要使用普通的 bash 配置文件继续终端,请添加exec bash
:
gnome-terminal --window-with-profile=Bash -- \
bash -c "<command>; exec bash"
exec
replaces the shell after running the command and can be excluded.
exec
运行命令后替换shell,可以排除。
If needing to pass something like a Here documentas the command, use the following (xterm is used here just as another example):
如果需要将类似Here 文档的内容作为命令传递,请使用以下命令(此处使用xterm 作为另一个示例):
cmd="$(printf '%s\n' 'wc -w <<-EOF
First line of Here document.
Second line.
The output of this command will be '15'.
EOF' 'exec bash')"
xterm -e bash -c "${cmd}"
The following can be used in a script to open a new terminal with the arguments as the command:
可以在脚本中使用以下命令以参数作为命令打开新终端:
nohup xterm -e bash -c "$(printf '%s\nexec bash' "$*")" &>/dev/null &
When $*
is quoted, it expands the arguments to a single word, with each separated by the first character of IFS. nohup
and &>/dev/null &
are used only to allow the terminal to run in the background.
当$*
被引用,其膨胀的参数到单个字,其中每个分离的由IFS的第一个字符。nohup
并且&>/dev/null &
仅用于允许终端在后台运行。