1

I've got a small script which is returning a string/path. This path is an executable, how can I run the executable? Thank you.

Example: my_command commands other commands ... returns /home/mydesktop/myexecutable I'd need to execute /home/mydesktop/myexecutable

1
  • An 'executable' is more of a permission than an atomic object the way an .exe sort of is in Windows. What program actually runs the executable? You just need to call its command-line arguments appropriately in your script most likely.
    – user1452106
    Jul 13, 2012 at 15:24

2 Answers 2

2

You could try this:

`your_command args etc`

The backticks get replaced by the output of the command and that is then evaluated. Since it is at the start of the input line, bash tries to execute it.

This is a handy trick to know, since you can use it for all sorts of fun:

cp your_file .backup/`date "+%Y-%m-%d"`_your_file

will prepend the current date to a copy of your file for poor mans backup...

EDIT: In the comments, we learned, that you should actually be using the $() syntax. So, that amounts to:

$(your_command args etc)

and

cp your_file .backup/$(date "+%Y-%m-%d")_your_file

since you can nest this...

3
  • What if I'm already using substitution in the command? How to use it two times? I have: mycommand args... other command with substitution args Thanks.
    – MoreOver
    Jul 13, 2012 at 15:38
  • 2
    @user1523953: This is the main reason you should use $() instead. You can nest $() as much as you want: $(echo $(do some command to $(the result of some command)))
    – sorpigal
    Jul 13, 2012 at 15:48
  • wow. this is good to know. maybe i should have read modern books, instead of learning unix from and old book by pike ;) Jul 16, 2012 at 9:38
0

If it returns an executable script/program use:

chmod +x /home/mydesktop/myexecutable
/home/mydesktop/myexecutable

If it returns an executable STRING use:

eval STRING

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.