Convert my bash command into a bashrc function and pass argument

Joe*_*her 4 linux bash shell command

I've got a sweet bash command to find key phrases in files and filepaths, thanks to @ezod here. I'm trying to turn it into a function in my bashrc, but it doesn't generate the same result. I'm guessing it's related to back-to-back curly brackets that I need to escape or use an alternative method?

Works:

{ find . -name '*keyword*'; grep -irl 'keyword' .; } | sort -u
Run Code Online (Sandbox Code Playgroud)

Does not work in bashrc:

function findit() {
  { find . -name '*$1*';
    grep -irl '$1' .;
  } | sort -u
}
export -f findit

$ findit keyword
Run Code Online (Sandbox Code Playgroud)

Bri*_*ite 5

Bash 不会用单引号替换变量。尝试这个:

function findit() {  
  { find . -name "*$1*";  
    grep -irl "$1" .;  
  } | sort -u  
}  
export -f findit  
Run Code Online (Sandbox Code Playgroud)

  • 请注意,grep 后面的`;` 不是必需的。只有在删除新行时才需要它。 (2认同)