0

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'

grp(){
     $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

4
  • You'd have to use eval "$1" but that's definitely not recommended. What's wrong with pac -Q | grep nvidia ? Commented Jun 24, 2020 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 Commented Jun 24, 2020 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
    Commented Jun 24, 2020 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 Commented Jun 24, 2020 at 16:55

3 Answers 3

2

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
hello

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 '

Then:

$ 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
2
  • That's not only in bash. Any standard shell should do that, and do it recursively: sh -ic $'alias foo="echo " bar="hello " baz=nasty\nfoo bar baz' => hello nasty.
    – user313992
    Commented Oct 6, 2020 at 6:14
  • @localuser ah, it is standard. Nice.
    – muru
    Commented Oct 6, 2020 at 6:32
0

You might try:

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

And invoke it like:

grp nvidia pac -Q
1
  • 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 Commented Jun 24, 2020 at 16:54
0

EDIT: I dun goofed. Nevermind.

A little old I know, but assuming you don't need to be widely portable across various older shell implementations, process substitution (instead of pipes) is one way to do this directly.

grp() {
  less -f <( grep ${@:2} <( $1 ))
}

invoke as grp 'pac -Q' nvidia, as requested.

3
  • 1
    (1) Look at the title of the question.  It is specifically about passing one alias to another.  Did you do this after defining pac as an alias?  I tried it and got bash: pac: command not found.  (2) Why ${@:2} instead of $2? Commented Oct 6, 2020 at 5:27
  • ...huh. To be honest, I tested it outside of a function definition... but you're right that makes a difference. That's awkward, my bad. (${@:2} is the positional parameters starting from $2)
    – user435972
    Commented Oct 7, 2020 at 1:13
  • Thanks; I know what ${@:2} is; I was asking why you thought it was appropriate, when the question is about $2. Commented Oct 7, 2020 at 1:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .