-2

I frequently use this command:

grep -rnw donkey | grep market | cut -c -80

How can I change it into an alias on Linux?

I tried:

alias search='grep -rnw $1 | grep $2 | cut -c -200'

But when I run it with

search hello world

it gives me:

cut: Usage: grep [OPTION]... PATTERN [FILE]...
hello: No such file or directory
cut: Try 'grep --help' for more information.
world: No such file or directory
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Ginger
  • 103
  • 6

1 Answers1

3

Unfortunately, bash alias doesn't accept parameters like some other shells.

But you can get around it by declaring search as a function which you can add to your .bashrc file

function search { grep -rnw $1 | grep $2 | cut -c -200; }
export -f search

See: Make bash alias that takes a parameter

In the link the example uses an alias which seems to be unessessary. You can put the function with export -f search directly into .bashrc and name the function as search.

If all else fails another option is to write a script called search.

e.g. create a script named search containing:

#!/bin/sh
grep -rnw $1 | grep $2 | cut -c -200;

Place this somewhere in a path defined in $PATH like /usr/bin or perhaps $HOME/bin. If using $HOME/bin put that path in your environment like.

export PATH=$PATH:$HOME/bin
hookenz
  • 14,472
  • 23
  • 88
  • 143