Patch-ID: bash41-003
[bash.git] / examples / functions / ksh-cd
blob801a4909a83114a021fc441c496ec0b83600d7e2
2 # ksh-like `cd': cd [-LP] [dir [change]]
4 cd()
6         OPTIND=1
7         while getopts "LP" opt
8         do
9                 case $opt in
10                 L|P)    CDOPTS="$CDOPTS -$opt" ;;
11                 *)      echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
12                         return 2;;
13                 esac
14         done
16         shift $(( $OPTIND - 1 ))
18         case $# in
19         0)      builtin cd $CDOPTS "$HOME" ;;
20         1)      builtin cd $CDOPTS "$@" ;;
21         2)      old="$1" new="$2"
22                 case "$PWD" in
23                 *$old*) ;;
24                 *)       echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
25                 esac
27                 dir=${PWD//$old/$new}
29                 builtin cd $CDOPTS "$dir" && echo "$PWD"
31                 ;;
32         *)      echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
33                 return 2 ;;
34         esac