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

1 Answer 1

2

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

e.g.

if [ $1 == "config" ]; then
5
  • Wow, thank you. That took care of it. I didn't realize it was that picky about spacing.
    – zxqx
    Jun 18, 2013 at 16:50
  • You are welcome. spacing around [ and ] helps bash with the parsing of the statement.
    – suspectus
    Jun 18, 2013 at 16:53
  • 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. Jun 18, 2013 at 16:56
  • @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.
    – zxqx
    Jun 18, 2013 at 16:58
  • 1
    I'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. Jun 18, 2013 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .