2 # Directory manipulation functions from the book 'The Korn Shell'
3 # Modified for use with bash Mon Apr 18 08:37 1994 by
4 # Ken Konecki (kenk@wfg.com)
6 # Modified by Chet Ramey
8 # This could stand to have calls to `select' added back in
11 alias integer="declare -i"
13 integer _push_max=${CDSTACK-31} _push_top=${CDSTACK-31}
18 # Display directory stack -- $HOME display as ~
32 while let "i < $_push_max"
35 eval "echo \$n\) \$_push_stack_$i"
40 # Change directory and put directory on front of stack
49 -[1-9]|-[1-9][0-9]) # cd -n
50 n=_push_top+${1#-}-1 type=2
53 1) # keep present directory
58 [2-9]|[1-9][0-9]) # cd n
59 n=_push_top+${1}-2 type=2
63 if let "_push_top <= 0"; then
69 if let "type < 3"; then
70 if let "n >= _push_max"; then
71 echo cd: Directory stack not that deep
74 eval dir=\${_push_stack_$n}
79 ~*) dir=$HOME${dir#\~} ;;
82 cd2 ${dir:-$@} > /dev/null || return 1
91 1) # swap first two elements
92 eval _push_stack_$_push_top=\$dir ;;
94 2|3) # put $dir on top and shift down by one until top
97 while let "i < $_push_max" ; do
98 eval _dirlist=\"\$_dirlist \$_push_stack_$i\"
103 for dir in "$dir" ${_dirlist} ; do
105 eval _push_stack_$i=\$dir
110 _push_top=_push_top-1;
111 eval _push_stack_$_push_top=\$dir
119 # Menu-driven change directory command
123 echo -n "Select by number or enter a name: "
129 # Emulate ksh cd substitution
133 0) builtin cd "$HOME" ;;
134 1) builtin cd "$1" ;;
135 2) newDir=$(echo $PWD | sed -e "s:$1:$2:g")
137 $PWD) echo "bash:: cd: bad substitution" >&2 ; return 1 ;;
138 *) builtin cd "$newDir" ;;
140 *) echo "bash: cd: wrong arg count" 1>&2 ; return 1 ;;