创建永久可执行别名

发布于 2024-10-19 15:25:59 字数 462 浏览 5 评论 0原文

我安装了 MySQL(MAMP、Mac OS X),但每次从 shell 访问它时都需要通过完整路径调用它。我创建了一个别名:alias mysql='/Applications/MAMP/Library/Bin/mysql,但这仅在我的终端/Bash 会话期间持续。

建立跨用户使用的永久别名的有效方法是什么? (我需要能够从 PHP 执行命令)。我应该在 Bash 启动脚本中设置别名(如何完成?),还是编辑 sudoers 文件更好? (也可以使用一个例子..)

谢谢

--编辑-基于答案:

我刚刚尝试创建一个~/.bashrc并编写了以下内容:

别名 mysql='/Applications/MAMP/Library/bin/mysql'

但这似乎没有任何效果。这个文件有特殊的语法吗?

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.

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..)

Thanks--

EDIT- Based on answer:

I just tried creating a ~/.bashrc and wrote the following:

alias mysql='/Applications/MAMP/Library/bin/mysql'

But this doesn't seem to have any effect. Is there a special syntax for this file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離殇 2024-10-26 15:25:59

将命令添加到您的 ~/.bashrc 文件中。

要使其可供所有用户使用,请将其添加到 /etc/profile 中。

Add the command to your ~/.bashrc file.

To make it available to all users, add it to /etc/profile.

年少掌心 2024-10-26 15:25:59
  • 不同的 shell 使用不同的点文件来存储别名。
  • 对于 Mac,bash shell 使用 .bash_profile.profile
  • 对于 ubuntu,bash shell 使用.bashrc
  • 如果您使用的是 zsh shell 和 ohmyzsh插件,点文件是 .zshrc

传统上,要添加永久别名,您需要打开点文件并手动写入别名,例如:

alias hello="echo helloworld"

并且记住获取点文件以使其采用 要在 ubuntu 的 bash 上获取点文件,请输入 source .bashrc 要使别名对所有用户可用,请写入 /etc/profile 而不是点文件。请记住输入 source /etc/profile 以使新别名生效。

如果您只是想要一个临时别名,则不需要写入点文件。只需在终端上输入相同的命令 (alias hello="echo helloworld)。

请注意,一旦 shell 启动,通过 alias 命令创建的临时别名就会消失。


如果您正在寻找无需打开文本编辑器即可生成别名的单个命令,请继续阅读。

您在 ubuntu 上安装了 ruby​​,则可以使用 aka 使用单个命令创建永久别名。

如果 安装 aka2

例如:

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.

使用 aka,无需使用文本编辑器写入点文件,也无需获取点文件。

  • 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

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.

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.

Note that a temporary alias created through the alias command will disappear once the shell is closed.


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.

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.

ゃ懵逼小萝莉 2024-10-26 15:25:59

你的处理方式是错误的。

/Applications/MAMP/Library/bin/ 添加到您的路径,或者创建一个脚本来调用 MySQL 并将其放置在您的路径中已有的 bin 目录中。

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.

滥情空心 2024-10-26 15:25:59

否则 .bashrc 文件不会获取源代码

在 Mac 上,除非将source ~/.bashrc 放入 /etc/profile中, /etc/bashrc.

只是想我会提到这一点。

On a mac the .bashrc file does not get sourced unless you put

source ~/.bashrc in the /etc/profile or /etc/bashrc.

Just thought I would mention that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文