0

I found it hard to come up with a suitable title. Here's what I'd like to have:

$ some_prefix deploy

Should run an executable called deploy in /some/directory/somewhere where I can substitute "deploy" for any other executable in that directory. I could add /some/directory/somewhere to my PATH, but some of the scripts have generic names so I'd like to call them with a prefix.

Jordan Sitkin
  • 2,303
  • 3
  • 26
  • 35
  • You should be able to use a function or shell script to accomplish this. – Kevin Jan 31 '17 at 20:31
  • which OS are you on? (or do you want a portable solution?). I think on OSX, 'some_prefix' would be 'open'. – philwalk Jan 31 '17 at 20:32
  • 1
    You could always just use store the directory in a variable and do something like `$some_prefix/deploy`. – Kevin Jan 31 '17 at 20:35
  • @Kevin yes I'm looking for help writing said function. Storing the prefix in a variable sounds like a fine idea, but I'd to be able to invoke `prefix deploy` at the prompt, rather than `$prefix/deploy` although thats pretty close – Jordan Sitkin Jan 31 '17 at 20:44
  • This just sounds like a function with a hard-coded path in the body. – chepner Jan 31 '17 at 20:45
  • @philwalk i'm on mac os. `open` is not what i'm looking for - the requirement is that i simply don't want to have to type out the full path to run those executables. – Jordan Sitkin Jan 31 '17 at 20:45
  • @chepner yes! can you provide an example of such a function? – Jordan Sitkin Jan 31 '17 at 20:46

3 Answers3

2

I think you just want a function that hard-codes a path:

run_it () {
    "/some/directory/somewhere/$1" "${@:2}"
}

Then call

run_it deploy foo bar baz

to run

/some/directory/somewhere/deploy foo bar baz
chepner
  • 497,756
  • 71
  • 530
  • 681
1

Make a function or alias called setmy, that will set a lot of alias functions based on the list of executables in /some/directory/somewhere.
Your setmy will perform alias mydeploy=/some/directory/somewhere/deploy for all executables.
Next call your function with prefix my, only skip the space:

mydeploy
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

You can temporarily redefine the value of PATH by prepending its definition to the command:

PATH=/some/directory/somewhere:/usr/local/bin:/usr/bin:/bin deploy

This can also be packed into an alias:

alias some_prefix='PATH=/some/directory/somewhere:$PATH'

So that you only need to run:

some_prefix deploy

The shell (bash, zsh or even dash), will first expand the alias

PATH=/some/directory/somewhere:$PATH deploy

and then the PATH parameter

PATH=/some/directory/somewhere:/usr/local/bin:/usr/bin:/bin deploy

before using the redefined PATH to search for the deploy command and also passing it onto the environment of the deploy process.


Note: The definition is scoped only to the following command, not to the whole command line. So, if you run

some_prefix deploy ; deploy

only the first deploy will be searched in the modified PATH, while the second uses the original value of PATH. The same holds true for commands that are chained with |, && or ||. If that is an issue, you might want to have a look at this question. Of course it is always possible to just use the alias multiple times as needed:

some_prefix deploy; some_prefix deploy
Community
  • 1
  • 1
Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • cool! i would have thought that the def would be scoped to the whole session. good trick to know. makes sense though now that i think about it. `export` would make it session wide right? – Jordan Sitkin Feb 01 '17 at 17:58
  • @DustMason Not exactly. If you just define a parameter on the command line without an added command, e.g. `NAME="VALUE"`, it already is scoped to the whole shell session. `export NAME` is only needed, if you want to make it available in the environment of any subsequent external commands started from that session. `export NAME="VALUE"` is just a shorthand that combines both. But `NAME="VALUE" command` is different in that it defines `NAME` only in the environment of `command`, it does not affect the running shell session in any way. Also, `export NAME="VALUE" command; command` will not work. – Adaephon Feb 02 '17 at 08:12