diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2013-08-18 01:14:53 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2013-08-18 01:14:53 +1200 |
commit | bd28ebe5789e51401a6e3a3ff266b458fe7afb9d (patch) | |
tree | 0e7e20e6157d98b2959739e10a2ed8bed1b8e016 /bash/bashrc.d/cd.bash | |
parent | Use printf in preference to echo (diff) | |
download | dotfiles-bd28ebe5789e51401a6e3a3ff266b458fe7afb9d.tar.gz dotfiles-bd28ebe5789e51401a6e3a3ff266b458fe7afb9d.zip |
Practicing writing half-decent Bash
Use array variable to collect options (appending with a pre-3.01
compatible syntax)
Diffstat (limited to 'bash/bashrc.d/cd.bash')
-rw-r--r-- | bash/bashrc.d/cd.bash | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/bash/bashrc.d/cd.bash b/bash/bashrc.d/cd.bash index 4adaafab..6c2e463f 100644 --- a/bash/bashrc.d/cd.bash +++ b/bash/bashrc.d/cd.bash @@ -1,20 +1,20 @@ # If given two arguments to cd, replace the first with the second in $PWD, # emulating a Zsh function that I often find useful; preserves options too __cd() { - local opts= + local -a opts while getopts elP opt; do - opts="$opts -$opt" + opts["${#opts[@]}"]="-$opt" done shift "$(($OPTIND-1))" if [[ "$#" -eq 2 ]]; then if [[ "$PWD" == *"$1"* ]]; then - builtin cd $opts "${PWD/$1/$2}" + builtin cd "${opts[@]}" "${PWD/$1/$2}" else printf 'bash: cd: could not replace substring\n' return 1 fi else - builtin cd $opts "$@" + builtin cd "${opts[@]}" "$@" fi } alias cd='__cd' |