Skip to content

Instantly share code, notes, and snippets.

@ysf
Last active April 19, 2016 12:29
Show Gist options
  • Select an option

  • Save ysf/50d50902e015b8cd3c30c6eb1f34913a to your computer and use it in GitHub Desktop.

Select an option

Save ysf/50d50902e015b8cd3c30c6eb1f34913a to your computer and use it in GitHub Desktop.
# source from .bashrc for lazy pipe grep magic
command_not_found_handle ()
{
# only run within an input pipe
if [[ -p /dev/stdin ]]; then
grep -- "$*"
return $?
fi
echo $"$1: command not found"
return 127
}
@algorythmic

Copy link
Copy Markdown

You prob only want to pass one argument to grep -- the string to search for. With "$@" you pass all the original args separately, so if you had more than one word they would be interpreted as file names to look in. I think "$*" makes more sense so you pass just one single arg to grep consisting of all the words you had after the pipe.

Also makes sense to pass -- first in case your string starts with a hyphen -- this way it won't be treated as an arg.

So like

grep -- "$*"

@ysf

ysf commented Apr 19, 2016

Copy link
Copy Markdown
Author

Yeah, thanks a lot @algorythmic. From time to time, I still use the args passing feature, but if i really need those I can still pipe and call grep directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment