47

I normally use ps -elf | grep proceesname to get a detailed description of the process named processname. I think that I have to write too much for this.

Now what i was thinking is to create a bash alias like

alias lsps='ps -elf | grep $1'

which will give the above detailed description only by using lsps processname.

So, my question is how do I create a bash alias which accepts an argument.

PS: I know I can write a shell script for the above task but I was just wondering how to do it with bash alias.

2
  • Aliases simply don't take arguments. Commented Jul 13, 2012 at 12:34
  • 1
    For GNU ps it's probably better to say lsps () { ps -lf -C "$1" ; }. Commented Jul 13, 2012 at 14:39

2 Answers 2

83

Very simple;

alias lsps='ps -elf | grep'

Command line arguments will be added automatically to the end of the alias:

lsps arg1 arg2 arg3 => converted to => ps -elf | grep arg1 arg2 arg3

That works only when you want to add arguments to the end of alias.

If you want to get arguments of the alias inside of the expanded command line you must use functions:

For example:

lsps()
{
    ps -elf | grep "$1" | grep -v grep
}

Functions as well as aliases can be saved in your ~/.bashrc file )or a file that is included from it):

$ cat /tmp/.bash_aliases
lsps()
{
    ps -elf | grep "$1" | grep -v grep
}

$ . /tmp/.bash_aliases
$
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks it worked. But what if I had to give an argument.
Just use functions in that case
That is strange. May be you've added additional spaces after the function name (in its definition). I added example of usage to my answer, as you can see all works perfect.
Yes, you have to add the function; and that is all.
Still not working. Here is my .bash_aliases and the error
|
7

Use this:

alias lsps='ps -elf | grep'

Then you can issue this:

lsps processname

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.