简体   繁体   中英

How to escape grep and awk within pipe in an alias?

I want to create an alias for an long command. But I'm not able to escape it correct, I guess it's a problem with the pipes.

My original command

ps aux | grep gimp | awk '{ print $2 '\011' $11 }' | grep -v 'grep'

My attempt for an alias

alias psa="ps aux | grep $1 | awk '{ print \$2 \"\011\" \$11 }' | grep -v 'grep'"

But I get an error that grep can not open file foo (when I do psa foo )
When I remove the last part | grep -v 'grep' | grep -v 'grep' then awk throws the same error.

I prefer an alias before an shell script.

You need to use a function if you want to to insert arguments:

psa() {
    ps aux | grep "$1" | awk '{print $2 "\t" $11 }' | grep -v grep
}

You can avoid all the escaping by using a function for this:

myps() {
   ps aux | grep gimp | awk '{ print $2 "\011" $11 }' | grep -v 'grep'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM