Creating a .bashrc function with encapsulated commands

2

I'm trying to create a function in .bashrc to shortcut my MAMP commands. I want to do something like this:

mamp config

  • Opens up MAMP's httpd.conf file for editing.

mamp restart

  • Restarts the MAMP server.

I created a function in .bashrc called mamp():

function mamp {
  if [$1 == "config"]; then
    nano /Applications/MAMP/conf/apache/httpd.conf
  fi

  if [$1 == "restart"]; then
    /Applications/MAMP/Library/bin/apachectl restart
  fi
}

But that doesn't seem to work.

I get this error -bash: [config: command not found

zakangelle

Posted 2013-06-18T16:40:59.910

Reputation: 123

Answers

2

I think all you need to do is add spaces in the if tests,

e.g.

if [ $1 == "config" ]; then

suspectus

Posted 2013-06-18T16:40:59.910

Reputation: 3 957

Wow, thank you. That took care of it. I didn't realize it was that picky about spacing. – zakangelle – 2013-06-18T16:50:00.590

You are welcome. spacing around [ and ] helps bash with the parsing of the statement. – suspectus – 2013-06-18T16:53:13.580

3@zakang: Well, [ is a command. Putting a space between [ and $1 is just like putting a space between mamp and config in mamp config. – user1686 – 2013-06-18T16:56:08.337

@grawity: That's good to know. I work with JavaScript on a daily basis, and it's much looser with stuff like this, so I just thought it was a formatting preference. – zakangelle – 2013-06-18T16:58:31.350

1I'd also make sure you have a parameter. [ $1 == "config" ] will choke somewhat cryptically if there is no $1 parameter. Putting [ $# -ne 0 ] && return will do so. – Rich Homolka – 2013-06-18T17:29:53.883