2

I want to pass a command line program by default when I run it.

myprog --foo=bar

The solution I could think of was to add a custom "myprog" wrapper to my "bin" folder

#! /bin/sh
/usr/bin/myprog --foo=bar $@

The good thing is that since I set up my system so the bin folder is in the PATH both for interactive shells and for GUI apps, calling myprog from anywhere will pass the --foo flag by default, including if I run myprog from the applications menu.

However, I didn't like that I had to use the absolute path to /usr/bin/myprog in my wrapper script (if I don't it enters an infinite loop). Is there a better way to go at this?

hugomg
  • 5,747
  • 4
  • 39
  • 54
  • 3
    Quote that "$@" otherwise it really won't do what you think it does. QUOTE YOUR VARIABLES !!!!!!! – Chris Davies Apr 22 '15 at 16:26
  • @muru: Maybe there is a solution that doesn't require a wrapper script? The answers there seem a bit too complex for something that ought to be simple... – hugomg Apr 22 '15 at 16:44
  • @roaima: what is the problem with not quoting the "$@" here? My intention is that separate arguments to ~/bin/myprog should become separate arguments for /usr/bin/myprog – hugomg Apr 22 '15 at 16:44
  • 2
    @hugomg pass one parameter that involves spaces and you'll find out. As for wrappers, that's why apps use configuration files and environment variables. If your app doesn't use either, you are out of luck. – muru Apr 22 '15 at 16:46
  • 2
    @hugomg if you don't quote $@ it's exactly the same as using $*, which re-splits words by whitespace even if they were initially quoted to protect them. See this and this for further detail. – Chris Davies Apr 22 '15 at 18:41

1 Answers1

0

You can use alias for that. From man sh:

Aliases An alias is a name and corresponding value set using the alias(1) builtin command. Whenever a reserved word may occur (see above), and after checking for reserved words, the shell checks the word to see if it matches an alias. If it does, it replaces it in the input stream with its value. For example, if there is an alias called “lf” with the value “ls -F”, then the input:

       lf foobar ⟨return⟩

 would become

       ls -F foobar ⟨return⟩

Aliases provide a convenient way for naive users to create shorthands for commands without having to learn how to create functions with arguments. They can also be used to create lexically obscure code. This use is dis‐ couraged.

Lambert
  • 12,680
  • If I put an alias in my .profile will it also count when launching apps from the applications menu instead of from an interactive shell? – hugomg Apr 22 '15 at 17:37