27

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?

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Yarin
  • 173,523
  • 149
  • 402
  • 512

4 Answers4

58

Add the command to your ~/.bashrc file.

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

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • @Dennis, thanks, but couldn't get this working- could you look at the edits to my question. – Yarin Feb 28 '11 at 04:00
  • 4
    @Yarin: Changes to the file won't have an effect until you start a new session or source the file into your current session using `. ~/.bashrc`. Furthermore, if you want to make this effective for more than one user, you'll either have to put it in each users' `.bashrc` or in the common `/etc/profile`. – Dennis Williamson Feb 28 '11 at 05:19
  • @Dennis- Edited /etc/profile, then restarted- this works for terminal actions, but is not recognized when I make a php system() call... any ideas? – Yarin Feb 28 '11 at 15:15
  • @Yarin: PHP has no access to aliases. Also, PHP uses `sh` rather than BASH. In your PHP script, just set a variable to the directory and name of the executable: `$mysql = "/Applications/MAMP/Library/bin/mysql"; system($mysql ...)` – Dennis Williamson Feb 28 '11 at 15:35
  • 1
    @Dennis- Did not know that about PHP- thanks... Is there a way to set the system-wide $PATH so that PHP would recognize it, per @meagar's answer? – Yarin Feb 28 '11 at 15:42
  • @Yarin: You could try adding a line like this `PATH=$PATH:/Applications/MAMP/Library/bin/mysql` in your `/etc/profile` and in your PHP script do `putenv("ENV=/etc/profile")` or `$_ENV["ENV"] = "/etc/profile"` before your `system()` call, but that didn't work for me. Also, in OS X, there are [other ways to set your `PATH`](http://serverfault.com/questions/16355/how-to-set-global-path-on-os-x) besides using `/etc/profile`. – Dennis Williamson Feb 28 '11 at 16:32
10
  • 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.

ytbryan
  • 2,644
  • 31
  • 49
6

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • @meagar- How can I add it to $PATH so that it will be system-wide, across users? For example, when I do $ export PATH=$PATH:/Applications/MAMP/Library/bin, it works for me in terminal, but not when I run php system(). – Yarin Feb 28 '11 at 15:27
  • What he is trying to say is to create `/usr/local/bin/mysql`, i.e. make a wrapper in a location which is already on the system-wide standard `PATH`, or just move the binary there (though the latter does not always work, if the binary expects to find some support file in the same directory). – tripleee Jan 23 '15 at 05:46
1

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.

Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86