Why is OPTIND messing my positional params?
问题 I have this function: sgrep () { local OPTIND; if getopts i o; then grep --color=auto -P -in "$1" "$2"; shift $((OPTIND-1)); else grep --color=auto -P -n "$1" "$2"; fi | sed -E -n 's/^([0-9]+).*/\1/p' | xargs -I{} vim +"{}" "$2"; stty sane } It should use grep case sensitive, if it is invoke with -i . But when it is, the it put -i is in place of search string and search string is in place of somefile : $ set -x $ sgrep 'somesearch' 'somefile' ---#output--- + sgrep -i 'somesearch' 'somefile' +