Skip to main content
added exception for zsh
Source Link
steeldriver
  • 81.9k
  • 12
  • 114
  • 164

YouSome shells allow aliases to be expanded anywhere in the command line - for example, zsh has the concept of a global alias, alias -g 'grep=grep --color=auto -H'

If your interactive shell is bash however, aliases are only expanded when they appear as the first word of a command, so you would need to wrap the grep call in a shell, and invoke the shell in a way that causes your ~/.bashrc file to be read, as though in an interactive shell session.

For example

find . -name '*.txt' -exec bash -ic 'for f do grep foo "$f"; done' find-bash {} +

AFAIK it's not unsafe in any way - however it has some overhead and is hardly more convenient than simply supplying the desired options to grep directly.

You would need to wrap the grep call in a shell, and invoke the shell in a way that causes your ~/.bashrc file to be read, as though in an interactive shell session.

For example

find . -name '*.txt' -exec bash -ic 'for f do grep foo "$f"; done' find-bash {} +

AFAIK it's not unsafe in any way - however it has some overhead and is hardly more convenient than simply supplying the desired options to grep directly.

Some shells allow aliases to be expanded anywhere in the command line - for example, zsh has the concept of a global alias, alias -g 'grep=grep --color=auto -H'

If your interactive shell is bash however, aliases are only expanded when they appear as the first word of a command, so you would need to wrap the grep call in a shell, and invoke the shell in a way that causes your ~/.bashrc file to be read, as though in an interactive shell session.

For example

find . -name '*.txt' -exec bash -ic 'for f do grep foo "$f"; done' find-bash {} +

AFAIK it's not unsafe in any way - however it has some overhead and is hardly more convenient than simply supplying the desired options to grep directly.

Source Link
steeldriver
  • 81.9k
  • 12
  • 114
  • 164

You would need to wrap the grep call in a shell, and invoke the shell in a way that causes your ~/.bashrc file to be read, as though in an interactive shell session.

For example

find . -name '*.txt' -exec bash -ic 'for f do grep foo "$f"; done' find-bash {} +

AFAIK it's not unsafe in any way - however it has some overhead and is hardly more convenient than simply supplying the desired options to grep directly.