bash 为所有用户全局设置别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30702803/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:09:04  来源:igfitidea点击:

Set aliases globally for all users

bashunix

提问by naveen kanni

I have certain paths, which are too long to type, so I need to wrap all those in one script as alias and source that script to my existing package code.

我有某些路径太长而无法键入,因此我需要将所有路径作为别名包装在一个脚本中,并将该脚本作为我现有的包代码的源。

These should set alias permanently over that server or over the package, so every user can use the alias instead typing the whole path.

这些应该在该服务器或包上永久设置别名,因此每个用户都可以使用别名而不是键入整个路径。

alias bc='bc -l'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i' 
alias grep='grep --color'
alias update='yum update'
alias update='yum -y update'

I have tried this but it isn't working.

我试过这个,但它不起作用。

回答by shivams

This is not the way to do this. Instead you need to set alias in your .bashrcfile.

这不是这样做的方法。相反,您需要在.bashrc文件中设置别名。

If you want these alias for one tty only then put them in a bash script and source that script on that tty. You only need to change shebang header in your script from

如果您只想要一个 tty 的这些别名,那么将它们放在一个 bash 脚本中,并在该 tty 上获取该脚本。你只需要在你的脚本中更改shebang标题

#!/usr/bin/perl to #!/usr/bin/bash

Instead if you want these changes in alias to be permanent then you need to put these entries in your .bashrc file and they will be permanent.

相反,如果您希望别名中的这些更改是永久性的,那么您需要将这些条目放入 .bashrc 文件中,它们将是永久性的。

If you want to make those changes for all users you can put that in /etc/bashrcor /etc/bash.bashrcwhichever is present on your system and then make /etc/profileto source it.

如果您想对所有用户进行这些更改,您可以将其放入系统中/etc/bashrc/etc/bash.bashrc系统上存在的任何一个,然后进行/etc/profile获取。

For example my machine is running ubuntu and it's /etc/profile entry is this way

例如,我的机器正在运行 ubuntu,它的 /etc/profile 条目是这样的

if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
fi

So if i put alias in /etc/bash.bashrc then they will be available to all users.

因此,如果我将别名放在 /etc/bash.bashrc 中,那么所有用户都可以使用它们。