i was trying to summerise grep while being able to give another command with parameters but I cannot figure out how to make it work What I am doing is this

alias pac='sudo pacman'

     $1 | grep $2 | less

And trying to run any of these:

grp 'pac -Q' nvidia
grp "pac -Q" nvidia
grp (pac -Q) nvidia

but i am failing in everyone of them,any advice?

TL;DR: Basically the tittle or Trying to make command -XYZ acceptable as an $NUM value or any other form usable the same way

  • You'd have to use eval "$1" but that's definitely not recommended. What's wrong with pac -Q | grep nvidia ? – glenn jackman Jun 24 at 16:13
  • pipe is in an awkward position in my keyboard so im trying to use it the least amount of time as possible,also the reason i am shortening it to grp is because i use grep quite a lot and the reason i posted this here(since grep is not enough of a reason for me to do it)is because I have a few other alias examples i want to shorten tbh I just cant find the "googleable words" to describe this phenomenon that I am trying to approach so I decided to post here – Jack Karavidas Jun 24 at 16:36
  • Aside: | less | grep is decidedly sub-optimal. Did you mean to do | grep | less? Or are you relying on less pre-processing some how? – muru Jun 24 at 16:52
  • You see,I have no idea why i wrote less first and then grep...thank you for pointing that out,though nothing changes in output since code was grep first then less,my post was wrong sorry – Jack Karavidas Jun 24 at 16:55

There might be a workaround. In bash, when an alias expansion ends with a space, the next word is also considered for expansion. So if I had:

alias foo='echo ' bar=hello

Then foo bar would result in echo hello being run:

$ foo bar

With this, you can use an alias for the grp function to have its first argument be considered for alias expansion, and then modify grp accordingly to use the last argument as the pattern:

grp () {
  local pat="${@: -1}"
  "${@:1:$#-1}" | grep "$pat" | less
alias grp='grp '


$ grp pac -Q nvidia
lib32-nvidia-utils 440.82-1
nvidia-dkms 440.82-2
nvidia-settings 440.82-1
nvidia-utils 440.82-2
opencl-nvidia 440.82-2

Of course, this won't work with complex aliases, for example those that use pipelines:

$ alias foo='bar | cat'
$ grp foo nvidia
cat: nvidia: No such file or directory
| improve this answer | |

You might try:

grp() {
    local grep_pattern=$1
    # remaining args are the command:
    "$@" | grep "$grep_pattern" | less

And invoke it like:

grp nvidia pac -Q
| improve this answer | |
  • hmm,unfortunately it dose not work with pac(another alias defined earlier on .bashrc ) but does work with sudo pacman -Q,further testing will be done for now,will update on any findings and wourld appreciate any further input as well – Jack Karavidas Jun 24 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.