4

Is it possible to create a symbolic link to an executable that executes it with a certain option/argument?

I know a workaround would be to create a bash script in one of the PATH directories but can I achieve it somehow with a symbolic link?

EDIT: Thanks for the answers, in my case an alias wouldn't do the job because i'm looking for a way to start matlab from dmenu and at least on arch matlab is only invokable from a terminal at first. Since dmenu does not consider aliases it wouldn't work .. i should have made my problem more clear.

  • If the executable is one which you are compiling, then you can do this, and it is in fact a common practice. By having multiple links (hard or soft) to an executable, and basing the behaviour on the value of $0, you get the effect you desire (where "which you are compiling" means writing code and compiling, not simply installing a standard package with ./configure ; make). – user4556274 Dec 20 '16 at 12:45
  • very interesting @user4556274 this is good to know ! I'm trying to invoke matlab with -desktop automatically when i invoke matlab. But i can't compile matlab myself :) so not possible in my case – philx_x Dec 20 '16 at 12:55
4

No, a symbolic link is a type of file that references the path of another file.

Now, if you do:

ln -s /bin/cat foo

And invoke foo as:

$ ./foo -A /proc/self/cmdline
./foo^@-A^@/proc/self/cmdline^@

You'll notice that the first argument that cat/foo received was ./foo and not cat. So, in a way, through that symlink, we've had cat receive a different first argument. That's probably not what you had in mind for your first argument though.

Using a shell script wrapper is the typical way to address it. You don't need to use bash for that though. Your system's standard sh will be more than enough for that:

#! /bin/sh -
exec /path/to/my/executable --extra-option "$@"

Other options include defining an alias or function in your ~/.bashrc/~/.zshrc... for it

  • nice, i guess "$@" will pass on the args i call the script with. – philx_x Dec 20 '16 at 12:47
0

From what i understand, you want let's say for example call script.sh and make it run script.sh arg1, so you can do that with alias, its like grep in linux, actually when you call it in ubuntu you calling grep --color=auto.

To do that create an alias, and put it in the right file depending on you distribution, for example in ubuntu you can put it in ~/.bashrc file

alias grep='grep --color=auto'
alias script.sh='script.sh arg1'

for more info: http://www.hostingadvice.com/how-to/set-command-aliases-linuxubuntudebian/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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