30

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?

0

4 Answers 4

60

Add the command to your ~/.bashrc file.

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

6
  • @Dennis, thanks, but couldn't get this working- could you look at the edits to my question.
    – Yarin
    Commented Feb 28, 2011 at 4: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. Commented Feb 28, 2011 at 5: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
    Commented Feb 28, 2011 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 ...) Commented Feb 28, 2011 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
    Commented Feb 28, 2011 at 15:42
11
  • 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.

0
7

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.

2
  • @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
    Commented Feb 28, 2011 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
    Commented Jan 23, 2015 at 5: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.

1
  • What about putting source ~/.bashrc into the ~/.profile? I don't have a Mac handy to do the experiment myself. Commented Jun 23, 2020 at 20:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.