Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to passing arguments to an alias [duplicate]

Tags:

alias

bash

i add an alias in .bashrc

alias sr='sudo /etc/rc.d/[parameter?] restart'

sr network -> sudo /etc/rc.d/network restart

sr sshd -> sudo /etc/rc.d/sshd restart

could it be achieved, thanks!

like image 839
toughtalker Avatar asked Dec 17 '10 03:12

toughtalker


People also ask

How do you pass arguments to alias commands?

You can replace $@ with $1 if you only want the first argument. This creates a temporary function f , which is passed the arguments. Alias arguments are only passed at the end. Note that f is called at the very end of the alias.

Can alias take arguments bash?

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 an 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.

Does Korn shell support aliases?

The Korn shell, or POSIX shell, allows you to create aliases to customize commands. The alias command defines a word of the form Name=String as an alias. When you use an alias as the first word of a command line, ksh checks to see if it is already processing an alias with the same name.


1 Answers

Use a shell function instead. eg:

function sr () {
  sudo /etc/rc.d/"$1" restart
}
like image 59
Laurence Gonsalves Avatar answered Oct 04 '22 11:10

Laurence Gonsalves