Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to get the following working:

alias banana='emacs /usr/local/nagios/etc/objects/'

So i want to just type "banana firewall.cfg" to edit this file(/usr/local/nagios/etc/objects/firewall.cfg). If i type that, emacs opens two buffers, one for "/usr/local/nagios/etc/objects" in directory edit mode, and the other just as a blank second file called firewall.cfg, which is expected, and obviously the command "bananafirewall.cfg" doesn't work.

I've been scratching my head for a good 30 minutes. Is alias even the right command for this? I guess I could ln -s all these files to /root, but any other suggestions?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Aliases are good for giving another name to a command, or for passing default arguments. They are not good beyond that, for example to modify an argument. Use a function instead.

To support multiple file names easily, change to the target directory first. Use parentheses instead of braces to create a subshell so that the directory change does not affect the parent shell.

banana () (
  cd /usr/local/nagios/etc/objects/ &&
  emacs "$@"
)
share|improve this answer
add comment

You might want to use a shell function like this:

banana () { emacs /usr/local/nagios/etc/objects/"$1"; }

Only works with one filename. To support more than one would make it more complicated.

share|improve this answer
    
This is the right answer. From bash manpage: There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used. –  cbliard Oct 23 '13 at 7:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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