I want to write an alias as such:

alias add="java -jar vc.jar name"

Is there a way I can use a wildcard for name and thus only have to type: add name - with name being any name of my choice? name being an argument.

  • Note that you should probably use an absolute path rather than the relative vc.jar, otherwise your alias will only work when invoked from a specific location. – Aaron Sep 26 at 12:25
up vote 5 down vote accepted

I think you are looking for functions.

 function add() {
   local name="$1"
   java -jar vc.jar "${name}"
 }

Add this to your ~/.bashrc or ~/.profile and just call like this;

user@host$ add samplename

Alternatively you can trigger an alias expansion by adding a space or tab character at the end of the alias definition.

alias add='java -jar vc.jar '

(Note the space at the end of definition).

Then just call it normally;

user@host$ add samplename

It should work.

EDIT: As pointed out by @kusalananda you can omit the space and it will still work just fine.

  • 5
    The space at the end of the alias should not be needed. – Kusalananda Sep 26 at 5:13
  • 4
    The space at the end of the alias is to trigger alias expansion of the next word. In foo bar, if foo is an alias ending with space, then bar is also checked for alias expansion. – muru Sep 26 at 5:27
  • They refer to wildcards in the question, which hints at filename expansion. Assuming that's what they intend to use, it's worth noting that the function here only uses its first argument, i.e. add *.txt just uses the first matching filename. – ilkkachu Sep 26 at 12:10
  • Adding the function above works, adding a space at the end of the line works as well..Thanks! – cdhdds Sep 26 at 14:09
  • If that works please mark as the answer for reference of others – cevhyruz Sep 28 at 1:57

You can try to remove the name flag and set an alias for java -jar vc.jar.

What you want to do:

alias add="java -jar vc.jar"

You now have set an alias. To run it:

add nameofsomethingyouwant

You could also try it with @cevhyruz's solution, but I think this one is simpler.

  • What's the su for? – Kusalananda Sep 26 at 9:27
  • @Kusalananda I don't really know, I just always have super user on my when I'm working in a terminal. Maybe if your permissions are not default, this could help or something. You don't have to do the su part. It's just a habit of me. – Sam Sep 26 at 9:33
  • 1
    Um, you have a habit to work as root in the terminal?! – Kusalananda Sep 26 at 9:33
  • @Kusalananda yup, always root – Sam Sep 26 at 9:39
  • 9
    There is absolutely no reason to change into root to run a Java program. Habitually working at an interactive root shell is not a practice that I would condone. – Kusalananda Sep 26 at 9:46

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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