简体   繁体   English

调用多个Bash别名

[英]Calling multiple Bash aliases

I want to open three different folders on three new terminals from my terminal using one command. 我想使用一个命令从终端在三个新终端上打开三个不同的文件夹。 All of them should run independently of each other meaning one command does not depend on the one before it. 它们都应彼此独立运行,这意味着一个命令不依赖于该命令。

Here is my .bash_aliases, which is called from .bashrc 这是我的.bash_aliases,从.bashrc调用

alias cmd1=gnome-terminal && cd ~/Desktop/

alias cmd2=gnome-terminal && cd ~/Documents/

alias cmd3=gnome-terminal && cd ~/Music/

alias runcmds='cmd1 & cmd2 & cmd3'

But this opens up three terminals in the Music directory and doesn't execute the commands correctly. 但这会在“音乐”目录中打开三个终端,并且无法正确执行命令。 How can I make it so that runcmds runs all 3 commands separately from each other? 如何使runcmds彼此独立运行所有3个命令?

Also, when do I need to use quotation marks and when do I not need to? 另外,什么时候需要使用引号,什么时候不需要?

Your order of operations is backwards: You need to cd before you start the terminals, if you want the new shells within those terminals to be impacted by the cd . 你的操作顺序是倒着:您需要cd启动终端之前 ,如果你想这些终端中的新壳由受到影响cd Moreover, you need to quote, to ensure that both commands -- the cd and the invocation of gnome-terminal -- are considered part of the alias. 此外,您需要引用以确保将cdgnome-terminal调用这两个命令都视为别名的一部分。

alias cmd1='cd ~/Desktop/ && gnome-terminal'
alias cmd2='cd ~/Documents/ && gnome-terminal'
alias cmd3='cd ~/Music/ && gnome-terminal'
alias runcmds='cmd1 & cmd2 & cmd3'

By the way, I'd suggest -- strongly -- not using aliases at all, and defining functions instead: 顺便说一句,我强烈建议不要使用别名,而是定义函数:

cmd1() { cd ~/Desktop && gnome-terminal; }
cmd2() { cd ~/Documents && gnome-terminal; }
cmd3() { cd ~/Music && gnome-terminal; }
runcmds() { cmd1 & cmd2 & cmd3 & }

No quoting involved whatsoever (the final & , as opposed to a final ; , prevents the parent shell's location to being changed to ~/Music by ensuring that cmd3 , like the others, runs in a subshell). 任何引用都不会涉及(final & (而不是final ;通过确保cmd3和其他cmd3一样在子shell中运行来防止将父shell的位置更改为~/Music )。 Of course, you could just implement one function: 当然,您可以只实现一个功能:

runcmds() {
  local dir
  for dir in ~/Desktop ~/Documents ~/Music; do
    (cd "$dir" && exec gnome-terminal) &
  done
}

Actually gnome-terminal facilitates the --working-directory option. 实际上, gnome-terminal有助于--working-directory选项。

From man gnome-terminal : man gnome-terminal

--working-directory=DIRNAME --working-directory = DIRNAME
Set the terminal's working directory to DIRNAME. 将终端的工作目录设置为DIRNAME。

You can use alias as follows: 您可以使用别名,如下所示:

alias cmd1='gnome-terminal --working-directory="$HOME/Desktop"'
alias cmd2='gnome-terminal --working-directory="$HOME/Documents"'
alias cmd3='gnome-terminal --working-directory="$HOME/Music"'
alias runcmds='cmd1 & cmd2 & cmd3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM