0

I am installing a AMP server in OSX (much easier in Ubuntu) using the MacPorts methods. I would like to add a bash script in my path called apachectl that will refer to /opt/local/apache2/bin/apachectl. I have been able to do this, but I was wondering how I can then pass parameters to apachectl that would then pass it to /opt/local/apache2/bin/apachectl?

e.g. apachectl -t >>> /opt/local/apache2/bin/apachectl -t

For those wondering why I don't just reorder my path, I was asking so that I could do the same thing with other commands, such ls -l which I currently have as ll (Ubuntu style) that looks like

ls -l $1

in the file.

Is the only way to do this why positional parameters such as what I have done above?

Luke Madhanga
  • 6,871
  • 2
  • 43
  • 47

1 Answers1

2

For what you want, you want to use "$@"

Explanation is from this answer that is in turn from this page

$@ -- Expands to the positional parameters, starting from one. 
When the expansion occurs within double quotes, each parameter 
expands to a separate word. That is, "$@" is equivalent to "$1" 
"$2" ... If the double-quoted expansion occurs within a word, the 
expansion of the first parameter is joined with the beginning part 
of the original word, and the expansion of the last parameter is 
joined with the last part of the original word. When there are no 
positional parameters,  "$@" and $@ expand to nothing (i.e., they are removed).

That would mean that you could call your ll script as follows:

ll -a /

"$@" will expand -a / into separate positional parameters, meaning that your script actually ran

ls -l -a /

You could also use a function:

apachectl() {
  /opt/local/apache2/bin/apachectl "$@"
}
Community
  • 1
  • 1
devnull
  • 118,548
  • 33
  • 236
  • 227