Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an alias of a command which accepts an argument and is executed on background?

I want to make alias to open file with GUI editor on background from command line.

I tried:

alias new_command="editor_path &"
new_command file


But it just open editor without loading the file.

like image 408
js_ Avatar asked Jul 14 '11 16:07

js_


People also ask

Can an alias take an argument?

Bash users need to understand that alias cannot take arguments and parameters. But we can use functions to take arguments and parameters while using alias commands.

How is an alias to argument created in the declare section?

Explanation: To declare an alias, the keyword ALIAS is used. Then, the colon sign followed by the name of ALIAS. Then, the name of the object is then specified whose alias is to be created. So, that the duplicate for that object can be created.


1 Answers

The & is ending the command, so it is not seeing your file argument. You can't use alias if you want to substitute the filename string into the command with the ampersand on the end.

From the bash manpage:

   There  is no mechanism for using arguments in the replacement text.  If
   arguments are needed, a shell function should be  used  (see  FUNCTIONS
   below).

Consider creating a shell function:

function new_command
{
editor_path "$1" &
}
like image 187
antlersoft Avatar answered Sep 21 '22 18:09

antlersoft