1

I use the command really often:

ack searchexpre -l --print0 | xargs -0 -n 1 sed -i -e sedexpression

I would like to create an alias like:

searchandreplace searchexpre  sedexpression

How I can do that?

Fractale
  • 1,503
  • 3
  • 19
  • 34
  • 1
    BTW -- please tag bash *or* zsh *or* sh, but not two or three of those at once. They're different shells, not mutually compatible with each other (except inasmuch as bash is *almost* a superset of POSIX sh but for its noncompliant `echo` implementation), and while in this particular case there's a single answer that works for all of them, (1) that's not always true, and (2) the excess tagging means that someone needs to test each answer in multiple shells to evaluate that answer's correctness. – Charles Duffy Jul 25 '17 at 01:21
  • Possible duplicate of [Make a Bash alias that takes a parameter?](https://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter) – codeforester Jul 25 '17 at 02:22
  • you should also consider using ag instead of ack it's about 10x faster in my testing; https://github.com/ggreer/the_silver_searcher – Calvin Taylor Aug 06 '17 at 09:19

1 Answers1

6

The rule of thumb with aliases is that if you have to ask, you should be using a function instead:

searchandreplace() {
    ack "$1" -l --print0 | xargs -0 -n 1 sed -i -e "$2"
}

You can now call searchandreplace searchexpre sedexpression

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • is there any way to give name to the parameter? like doint searchandreplace(arg1,arg2) { .... } ? – Fractale Jul 25 '17 at 01:25
  • 1
    No, `sh` and `bash` do not support named parameters. You can assign `$1` and `$2` to variables with whichever names you want though – that other guy Jul 25 '17 at 01:28