Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating permanent executable aliases

Tags:

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?

like image 524
Yarin Avatar asked Feb 28 '11 02:02

Yarin


People also ask

How do I create a permanent alias on Mac?

Create an alias On your Mac, do one of the following: Select the item, then choose File > Make Alias. You can create as many aliases for an item as you want, then drag them to other folders or to the desktop.


2 Answers

Add the command to your ~/.bashrc file.

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

like image 104
Dennis Williamson Avatar answered Oct 14 '22 22:10

Dennis Williamson


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

like image 31
ytbryan Avatar answered Oct 14 '22 22:10

ytbryan