Linux 创建永久可执行别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5137726/
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
Creating permanent executable aliases
提问by Yarin
I have MySQL installed (MAMP, Mac OS X) but need to call it by the full path each time I access it from the shell. I created an alias: alias mysql='/Applications/MAMP/Library/Bin/mysql
, but this only lasts as long as my terminal/Bash session.
我安装了 MySQL(MAMP,Mac OS X),但每次从 shell 访问它时都需要通过完整路径调用它。我创建了一个别名:alias mysql='/Applications/MAMP/Library/Bin/mysql
,但这仅与我的终端/Bash 会话一样长。
What is an effective way for establishing permanent aliases that will work across users? (I need to be able to execute commands from PHP). Should I set up aliases in the Bash start up script (how is that done?), or is it better to edit the sudoers file? (Could use an example of that as well..)
建立可跨用户使用的永久别名的有效方法是什么?(我需要能够从 PHP 执行命令)。我应该在 Bash 启动脚本中设置别名(这是怎么做的?),还是编辑 sudoers 文件更好?(也可以举个例子..)
Thanks--
谢谢 -
EDIT- Based on answer:
编辑-基于答案:
I just tried creating a ~/.bashrc
and wrote the following:
我只是尝试创建一个~/.bashrc
并写了以下内容:
alias mysql='/Applications/MAMP/Library/bin/mysql'
别名 mysql='/Applications/MAMP/Library/bin/mysql'
But this doesn't seem to have any effect. Is there a special syntax for this file?
但这似乎没有任何影响。这个文件有特殊的语法吗?
采纳答案by Paused until further notice.
Add the command to your ~/.bashrc
file.
将命令添加到您的~/.bashrc
文件中。
To make it available to all users, add it to /etc/profile
.
要使其可供所有用户使用,请将其添加到/etc/profile
.
回答by meagar
You're going about this the wrong way.
你这样做是错误的。
Either add /Applications/MAMP/Library/bin/
to your path, or create a script to invoke MySQL and place it in a bin
directory which is already in your path.
添加/Applications/MAMP/Library/bin/
到您的路径,或创建一个脚本来调用 MySQL 并将其放置bin
在您的路径中已经存在的目录中。
回答by user3347613
On a mac the .bashrc
file does not get sourced unless you put
在 Mac 上,.bashrc
除非您输入,否则文件不会被获取
source ~/.bashrc
in the /etc/profile
or /etc/bashrc
.
source ~/.bashrc
在/etc/profile
或 中/etc/bashrc
。
Just thought I would mention that.
只是想我会提到这一点。
回答by ytbryan
- Different shell uses different dot file to store aliases.
- For mac, the bash shell uses
.bash_profile
or.profile
- For ubuntu, the bash shell uses
.bashrc
- If you are using zsh shell and ohmyzsh plugin, the dot file is
.zshrc
- 不同的 shell 使用不同的点文件来存储别名。
- 对于 mac,bash shell 使用
.bash_profile
或.profile
- 对于 ubuntu,bash shell 使用
.bashrc
- 如果您使用的是 zsh shell 和 ohmyzsh 插件,则点文件为
.zshrc
Traditionally, to add a permanent alias, you need to open the dot file and write alias manually like:
传统上,要添加永久别名,您需要打开点文件并手动编写别名,例如:
alias hello="echo helloworld"
And remember to source the dot file to have it take effect.To source the dot file on ubuntu's bash, type source .bashrc
To make the alias available to all users, write to /etc/profile
instead of the dot file. Remember to type source /etc/profile
for the new alias to take effect.
并记住获取点文件以使其生效。要在 ubuntu 的 bash 上获取 dot 文件,请键入source .bashrc
To make the alias 对所有用户可用,/etc/profile
而不是写入dot 文件。请记住键入source /etc/profile
以使新别名生效。
If you simply want a temporary alias, you do not need to write to dot file. Simply type that same command (alias hello="echo helloworld
) on the terminal.
如果您只是想要一个临时别名,则无需写入点文件。只需alias hello="echo helloworld
在终端上键入相同的命令 ( )。
Note that a temporary alias created through the alias
command will disappear once the shell is closed.
请注意,alias
一旦关闭外壳,通过该命令创建的临时别名将消失。
If you are looking for a single command to generate aliases without open the text editor, read on.
如果您正在寻找无需打开文本编辑器即可生成别名的单个命令,请继续阅读。
If you have ruby installed on ubuntu, you can create permanent alias with a single command using aka.
如果您在 ubuntu 上安装了 ruby,则可以使用 aka 使用单个命令创建永久别名。
gem install aka2
gem install aka2
For example:
例如:
aka generate hello="echo helloworld" #will generate a alias hello="echo helloworld"
aka destroy hello #will destroy the alias hello
aka edit hello #will prompt you to edit the alias.
With aka, there is no need to write to the dot file with a text editor. And no need to source the dot file too.
使用 aka,无需使用文本编辑器写入点文件。也无需获取点文件。