Reputation: 5618
I want to use both bash alias and bash function with several arguments. I emulate svn sub commands.
$ svngrep -nr 'Foo' .
$ svn grep -nr 'Foo' .
My expectation is both act as below:
grep --exclude='*.svn-*' --exclude='entries' -nr 'Foo' .
But actual, only alias ('svngrep') does well, function ('svn grep') causes invalid option error. How to write my .bashrc?
#~/.bashrc
alias svngrep="grep --exclude='*.svn-*' --exclude='entries'"
svn() {
if [[ $1 == grep ]]
then
local remains=$(echo $@ | sed -e 's/grep//')
command "$svngrep $remains"
else
command svn "$@"
fi
}
Upvotes: 0
Views: 1786
Reputation: 5618
I re-factor this by myself. And work fine! Thanks!
#~/.bashrc
alias svngrep="svn grep"
svn() {
if [[ $1 == grep ]]
then
local remains=$(echo $* | sed -e 's/grep//')
command grep --exclude='*.svn-*' --exclude='entries' $remains
else
command svn $*
fi
}
I choice I keep alias simple. And I use $* instead of $@.
Edited: 2012-06-11
#~/.bashrc
alias svngrep="svn grep"
svn() {
if [[ $1 = grep ]]
then
shift
command grep --exclude='*.svn-*' --exclude='entries' "$@"
else
command svn "$@"
fi
}
Upvotes: 0
Reputation: 246774
You want shift
to remove the first word from the positional parameters: this preserves the array-like nature of "$@"
.
svn() {
if [[ $1 = grep ]]; then
shift
svngrep "$@"
else
command svn "$@"
fi
}
With bash's [[
builtin, single =
is used for string equality and double ==
is used for pattern matching -- you only need the former in this case.
Upvotes: 2
Reputation: 17117
svngrep
is not a variable. It's an alias used by bash. Therefore have to create a new variable like:
svngrep_var="grep --exclude='*.svn-*' --exclude='entries'"
And use it in your snippet:
...
command "$svngrep_var $remains"
...
Upvotes: 0