Linux alias with params

Passing argument to alias in bash [duplicate]

An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1 ).

$ alias foo='/path/to/bar' $ foo some args 

If you want to use explicit arguments, you’ll need to use a function

will be executed as if you had done

$ /path/to/bar abc 123 fixed args 

To see the type and definition (for each defined alias, keyword, function, builtin or executable file):

Or type only (for the highest precedence occurrence):

@10basetom: If you’re referring to the colors in my answer, that’s done automatically by the syntax highlighter.

to use parameters in aliases, i use this method:

alias myalias='function __myalias() < echo "Hello $*"; unset -f __myalias; >; __myalias' 

its a self-destructive function wrapped in an alias, so it pretty much is the best of both worlds, and doesnt take up an extra line(s) in your definitions. which i hate, oh yeah and if you need that return value, you’ll have to store it before calling unset, and then return the value using the «return» keyword in that self destructive function there:

alias myalias='function __myalias() < echo "Hello $*"; myresult=$?; unset -f __myalias; return $myresult; >; __myalias' 

you could, if you need to have that variable in there

alias mongodb='function __mongodb() < ./path/to/mongodb/$1; unset -f __mongodb; >; __mongodb' 
alias mongodb='./path/to/mongodb/' 

would actually do the same thing without the need for parameters, but like i said, if you wanted or needed them for some reason (for example, you needed $2 instead of $1), you would need to use a wrapper like that. If it is bigger than one line you might consider just writing a function outright since it would become more of an eyesore as it grew larger. Functions are great since you get all the perks that functions give (see completion, traps, bind, etc for the goodies that functions can provide, in the bash manpage).

I hope that helps you out 🙂

Источник

bash alias with parameters [duplicate]

I would like to swap from csh to bash, then I have to set the .bashrc with the commands I use. Translating alias with parameters seems to be not easier as I believed. csh:

alias gr 'xmgrace -legend load -nxy \!* -free -noask&' 
alias gr='xmgrace -legend load -nxy $@ -free -noask&' alias gr='xmgrace -legend load -nxy $(@) -free -noask&' 
alias t 'set t=\`pwd\``;echo $t' alias tt 'cd $t' 

@muru I was looking for a suitable dupe but couldn’t find one..the answer from the one you have referred to does not provide the whole scenario IMHO..

Читайте также:  Responder kali linux перехват хешей

@muru well, i have tried to make it more clarified. if you don’t feel the same its allright..i thought i should tell you what i felt prior to answering, nothing more 🙂

1 Answer 1

This is not the way bash aliases work, all the positional parameters in the bash aliases are appended at the end of the command rather than the place you have defined. To get over it you need to use bash functions.

An example will make you more clear :

$ cat file.txt foo $ cat bar.txt foobar spamegg $ grep -f file.txt bar.txt foobar $ alias foo='grep -f "$1" bar.txt' ## Alias 'foo' $ foo file.txt grep: : No such file or directory $ spam () < grep -f "$1" bar.txt ;>## Function 'spam' $ spam file.txt foobar 

As you can see as the first argument in case of alias foo is being added at the end so the command foo file.txt is being expanded to :

while in case of function spam the command is correctly being expanded to :

So in your case, you can define a function like :

Источник

How to pass parameters to an alias?

If you’re really against using a function per se, you can use:

$ alias wrap_args='f()< echo before "$@" after; unset -f f; >; f' $ wrap_args x y z before x y z after 

You can replace $@ with $1 if you only want the first argument.

Explanation

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.

The unset -f removes the function definition as the alias is executed so it doesn’t hang around afterwards.

This is great for special cases. Consider a block of code where you want to prevent echo with minimal fuss: alias echo=’f()< echo "$@" &>/dev/null; unset -f f; >; f’ . For example, adding that one line to a boot script to force a quiet mode.

This looks like a useless complication that still has many of the downsides of an alias, like the quoting hell that comes up if the function code itself needs to use single-quoted strings. Just defining the function the usual way wouldn’t suffer that problem, and wrap_args()< echo before "$@" after; >; would be shorter to boot, and there wouldn’t be name clashes with the temporary function.

Aliases are like commands in that all arguments to them are passed as arguments to the program they alias. For instance, if you were to alias ls to ls -la , then typing ls foo bar would really execute ls -la foo bar on the command line.

If you want to have actual control over how the arguments are interpreted, then you could write a function like so:

opengroup.org/onlinepubs/009695399/utilities/… Functions in sh are defined my_program_wrapper() < . ; >. Bash does handle the keyword function but why not go with what’s more portable?

Читайте также:  Canon lbp 3010 linux drivers

@SridharSarnobat If by export you mean export -f my_program_wrapper see this question for an explanation, then the answer is no; you will want to add the function to ~/.bashrc in order to have it usable in interactive shells.

Adding to the present answers, an important thing to realize about how aliases work is that all the parameters you type after an aliased command will be used literally at the end. So there is no way to use alias for two commands (piped or not), out of which the first should interpret the parameters. To make it clear, here’s an example of something that would not work as expected:

alias lsswp="ls -l | grep swp" 

(an example inspired by this question) this will always use the output of ls -l performed in the current directory and do a grep on that — so using

would be equivalent to ls -l | grep swp /tmp/ and not ls -l /tmp/ | grep swp .

For all purposes where the arguments should be used somewhere in the middle, one needs to use a function instead of an alias.

You don’t have to do anything, actually; aliases do this automatically. For instance:

$ alias less="less -eirqM" $ less foo.txt 

You will see foo.txt’s first page, and less will quit at EOF (-e), searches will be case-insensitive (-i), etc.

Can you please look at my question — none of the solutions works for me and I am not allowed start a bounty.

@JJD The one who answered your question is correct: you want a function in that case, not an alias. Since the accepted answer to this question is essentially the same, your question was rightly closed.

Yes, you can use the parameters in aliases and — as a difference to what has been said above — you can refer to them anywhere in the definition of alias — not only at the end.

Example for tar-gz -ing something:

$ alias tgz "tar cvf - \!:1 | gzip -9 > \!:2.tar.gz" 

, where !:1 and !:2 are the parameters you will supply when calling your alias.

 $ ls clrcf.dat user_comment_2016.06.03_12:51:50.txt user_comment_2016.06.03_12:54:48.txt TEST-wADM.tec user_comment_2016.06.03_12:52:04.txt user_comment_2016.06.03_12:55:13.txt $ tgz user* out a user_comment_2016.06.03_12:51:50.txt 1K a user_comment_2016.06.03_12:52:04.txt 1K a user_comment_2016.06.03_12:54:48.txt 1K a user_comment_2016.06.03_12:55:13.txt 1K $ ls out* out.tar.gz 

Which effectively means that you used two parameters that you inserted at arbitrary places of the tar command, making of all of it an alias tgz

Источник

Can I pass arguments to an alias command?

Now d will print the last 5 lines. If I want to use d to print a different number of lines, I have to make change in the alias command declaration of d again. Is there any way I can modify the declaration of an alias so that I don’t have to retype the declaration to change the number of lines. Like incorporating passing the number of lines as an argument while declaring alias for d ? Or is there some other method to solve this?

Читайте также:  Ndg linux exam answers

In (t)csh, «\!*» references arguments to an alias (the backslash is just to escape the exclamation mark which normally means «history»), and you can even reference individual arguments, though I don’t remember how. So perhaps «tail -n \!*» or something (I don’t think \!* will work with a minus sign immediately before it). However, not sure if this will work in (ba)sh.

4 Answers 4

Aliases don’t take arguments. With an alias like alias foo=’bar $1′ , the $1 will be expanded by the shell to the shell’s first argument (which is likely nothing) when the alias is run.

So: Use functions, instead.

d () < num=$dmesg |grep -iw usb|tail -$num > 

num=$ uses the first argument, with a default value of 5 if it isn’t provided.

$ d # prints 5 lines $ d 10 # prints 10 lines 

Or, if you change the options you used slightly:

alias d="dmesg|grep -iw usb|tail -n 5" 

Then you can pass additional -n options:

$ d # prints 5 lines $ d -n 10 # prints 10 lines 

If multiple -n options are specified for tail , only the last is used.

For the functionally challenged like myself 🙂 it might be helpful to briefly state where to put the function. ie ~/.bashrc or rc.local or wherever?

You need to have a function for this as described in the SO and here. Try the following:

Working around alias limitations with group command and here-string

Aliases can’t take arguments, but we can «simulate» that. Take for instance example of my answer on this question.

Key points that are happening here:

  • we use read built in to read a string into a variable d . Because we want to read a full string including blank characters(newline’s,tabs,spaces), we use IFS= and disable back backslash escapes with -r .
  • multiple commands within alias are combined and executed in current shell using < list; >structure (which is known as group command in the bash manual). Note that leading space after < and ; individual list of commands are required.

In your specific example we could do:

We could also make use of word splitting to store space-separated arguments:

Or we could use arrays to provide multiple arguments:

But is this good approach ?

Not necessarily. The problem with such approach is that it’s very specific — arguments can’t be quoted easily which means we can only have arguments with no spaces.

This is of course not something that would be widely used, simply because in the real world we have to deal with complex arguments, so this approach isn’t quite practical. Functions are far more flexible. And the need to quote args string becomes annoying fast.

Despite the limitations, this works with simple strings as arguments where we can afford word splitting, thus partially allows us to give arguments to aliases.

Источник

Оцените статью
Adblock
detector