Invalid grep

execute the following command:

ps aux | grep-n redis-server | grep-v grep

the process information of redis-server can be found normally.
(Note: grep-v grep is to exclude the process information of grep itself from the results)

but write it as an alias:

alias pg= "ps aux | grep-n $@ | grep-v grep" ,

then execute:

pg redis-server

prompt as follows:

grep: redis-server: No such file or directory

could you tell me what"s wrong? Thank you!


just change the order

alias pg='ps aux|grep -v grep| grep -n $@'

for $@ , the symbol is followed by a parameter

$ alias pg="ps aux|grep -n $@|grep -v grep"
$ set -x  -sharpbash
PP update_terminal_cwd
PP local url_path=
...

$ pg abc
+ ps aux
+ grep -n
+ grep -v grep abc
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
    [-e pattern] [-f file] [--binary-files=value] [--color=when]
    [--context[=num]] [--directories=action] [--label] [--line-buffered]
    [--null] [pattern] [file ...]
grep: abc: No such file or directory
PP update_terminal_cwd
PP local url_path= 

as you can see, the result of pg abc execution is

ps aux
grep-n
grep-v grep abc

that is, grep-v grep is also part of the $@ representation and is replaced by bash parsing.


in fact, you can't do this simply because you use double quotes. When you use double quotes, the position function $@ has already been expanded in alias. Of course, the value cannot be passed in. Try changing it to single quotes.

pot, pot, really not ha, last night at home can not verify, a little take it for granted. Come to the company in the morning ~ so let's explore why it doesn't work.

there is a sentence about position parameters in the man document of bash:

The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).
when the shell function executes, the position parameter is temporarily replaced

that is, when no function is called, the position variable is always the parameter passed when bash is started, and the default value is null.

in the answer of the brother upstairs, the position parameter is also valid without using the position parameter

alias pg='ps aux|grep -v grep| grep -n'

if you want to use position parameters, you can use functional writing, or use functions to write aliases. source and then use

directly.
-sharp .bashrc
pg(){ ps aux|grep -n $@ | grep -v grep; }
-sharp 
alias pg='f(){ ps aux | grep -n $@ | grep -v grep;}; f'

of course, change the position directly, and the way you write it to the brother upstairs is also OK.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1c22042-3b97c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1c22042-3b97c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?