0

How could I run this command as an alias?

alias findsum='find /home/sams/ -name sum.txt -exec grep -H "$1" {} \;'

This should be very simple, but I am not able to make it work.

findsum test
find: paths must precede expression: `test'
user1156544
  • 349
  • 2
  • 9
  • 4
    Bash aliases don't accept arguments - see [In Bash, when to alias, when to script, and when to write a function?](https://unix.stackexchange.com/questions/30925/in-bash-when-to-alias-when-to-script-and-when-to-write-a-function) – steeldriver May 17 '21 at 19:36
  • Mmm.. they do. I use them often – user1156544 May 17 '21 at 19:43
  • I can confirm bash aliases _do_ accept argument...and most use it without even noticing too :) (eg: like the `ls` alias on most distro, which most use additional argument with...) @steeldriver – Nordine Lotfi May 17 '21 at 19:47
  • 4
    @NordineLotfi The alias doesn't accept an argument. The alias is expanded without any consideration for what follows. _Then_ the arguments are passed to the command. – Gilles 'SO- stop being evil' May 17 '21 at 19:49
  • So, how can I make this work `alias findsum='find /home/sams/ -name sum.txt -exec grep -H "$1" {} \;'` I can't find an answer in the linked post and someone decided to close the question – user1156544 May 17 '21 at 19:52
  • You're right. misunderstood what was meant by the word "argument" :D @Gilles'SO-stopbeingevil' – Nordine Lotfi May 17 '21 at 19:55
  • @user1156544 the linked question gives you the answer - you can't do this with an alias (because aliases can't take arguments), you need to write `findsum` as a function. e.g. `findsum () { find /home/sams/ -name sum.txt -exec grep -H "$1" {} + ; }`. – cas May 18 '21 at 01:57

0 Answers0