0

I need help, I can't create an alias with a variable to grep a file on a remote server.

I have tried:

alias searchword="ssh -t user@server "grep -i \"$1\" /root/file.txt\""

alias search="ssh -t user@server 'grep -i $1 '\'"/root/file.txt\'"

If I send the command from the console it works, the problem comes when creating an alias.

Can you help me?

choroba
  • 306
  • 1
  • 6
xxmlud
  • 3
  • 3

1 Answers1

0

Aliases don't take arguments. Use a function instead:

searchword () {
    ssh -t user@server "grep -i '$1' /root/file.txt"
}

Note that it is dangerous, it can lead to uncontrolled code execution. Imagine what happens then the argument is '; rm -rf / ; #

choroba
  • 306
  • 1
  • 6
  • Perfect! I didn't know aliases couldn't use arguments. I used it and it worked perfectly. Greetings and thank you very much! – xxmlud Jul 06 '19 at 20:11
  • Alias just replaces the first word with the given string. The remaining words can be used as arguments, but it only works if all the arguments are at the end of the command, which isn't the case of `grep`'s pattern. – choroba Jul 06 '19 at 20:15