bash alias help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash alias help
# 1  
Old 06-12-2008
bash alias help

I try the following alias:
alias psx='ps auxw | grep $1 | grep -v grep'
but it fails with "grep: yyy: No such file or directory" (for 'psx yyy' command).

the following is ok:
alias psx='ps auxw | grep $1'

why this happens?
how to overcome this?

thanks
# 2  
Old 06-12-2008
An alias cannot have an argument like $1. You want to use a function instead.

Code:
psx () {
  ps auxw | grep "$1" | grep -v grep
}

Your second alias works by coincidence when $1 is empty in the current shell (as it will often be in an interactive bash); it expands to ps auxw | grep (empty string) yyy -- and ironically, if you had double-quoted $1 properly, it would also not have worked.

Your first alias expands to ps auxw | grep (empty string) | grep -v grep yyy

Search these forums for ways to avoid the grep -v grep elegantly; solutions have been posted a number of times.
# 3  
Old 06-12-2008
You might it helpful to investigate the pgrep command if it is available on your system (I think it is on Solaris and Linux).
# 4  
Old 06-12-2008
thanks!

Apparently many make this mistake, I found more than once $1's for aliases in .bashrc files...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Alias does not work with bash profile

Hi, Below is what i have in my profile: alias wldm='cd /opt/app/wls' If i use bash or ksh shell this alias does not work. What should be done for this alias to work with all these simultaneously -> No Shell, bash shell, and ksh shell (14 Replies)
Discussion started by: mohtashims
14 Replies

2. Shell Programming and Scripting

Creating .../ alias in bash

I want to create an alias as follows but is not working alias ../='cd ../' (3 Replies)
Discussion started by: kristinu
3 Replies

3. Shell Programming and Scripting

Not able to create alias in bash Sun solaris

Hello Friends, I am facing problem in creating aliases. I am working in SunOS 5.10. When I login in system i have been given ksh shell. What i am doing is that ? I am changing shell to bash then I am creating 2 alias in command prompt like: alias deploy_dir=' cd... (4 Replies)
Discussion started by: krsnadasa
4 Replies

4. Shell Programming and Scripting

Suppressing carriage return in bash alias

I'd like to create an alias that displays my string but leaves my cursor at the end. Not seeing any examples of this. One indirect way might be to preload or stuff the history buffer, so I just hit up arrow. (2 Replies)
Discussion started by: tns1
2 Replies

5. OS X (Apple)

Where should I install an alias file under UNIX bash ?

I have a file named 'aliases' which contains a list of aliases like: alias loc='locate' alias h='history' alias .='pwd' alias ..='cd ..' alias cd..='cd .. ; pwd' .............................. Where should I put this file (alias) so when Terminal launches .bash and thus a shell at... (1 Reply)
Discussion started by: shub22
1 Replies

6. Shell Programming and Scripting

how can I export an alias and use it in any shell that I want to switch to like bash, ksh

None of the aliases that I set are available if I switch to a different shell. How can I export aliases and make them available in any shell that I switch to like ksh or bash ? I tried these $>alias godata='cd /home/kc/app/data' $>alias -x godata='cd /home/kc/app/data' $>alias |... (2 Replies)
Discussion started by: kchinnam
2 Replies

7. UNIX for Dummies Questions & Answers

BASH: Change alias to script to add a task

Hi. I use an alias, "homeperm" as shorthand for curl -o. Since most of what I download via cUrl is graphic image files -- jpeg files -- I'd like to be able to change this alias to a script, or use it to invoke a function, which will not only download the file but date-stamp it using Exiv2 in... (4 Replies)
Discussion started by: SilversleevesX
4 Replies

8. Shell Programming and Scripting

Bash alias for complicated ls command does not work.

I'm trying to set up an alias in .bash_aliases to show just the filenames of the files in a directory, which the following command will do: ls -l | grep ^- | awk '{print $NF}' kjb.zip ap.zip tor.zip However when I set up the following alias in .bash_aliases: alias lf="ls -l | grep ^- |... (16 Replies)
Discussion started by: gencon
16 Replies

9. UNIX for Dummies Questions & Answers

alias in bash shell for CTRL + l

Is it possible to create an alias wherein it will use a keystroke. Like to clear the screen in bash i have to use CTRL + l. I want to make an alias 'c' out of this. Thanks. (6 Replies)
Discussion started by: or_knob
6 Replies

10. UNIX for Dummies Questions & Answers

Query regarding alias and setting bash as a default script

Hi All, I am setting bash as my working shell in my .profile file. So I have written a line : bash as the list line in my .profile I want to use alias as follows: alias me='who am i' When i log in, as expeced I enter the bash shell but alias doesn't work. Is it because the alias is defined... (1 Reply)
Discussion started by: VENC22
1 Replies
Login or Register to Ask a Question