diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2014-10-05 12:26:28 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2014-10-05 12:26:28 +1300 |
commit | 553976c1505a391203f078a81b93aa9abb99ebe6 (patch) | |
tree | 98a04ad10b3fae45b89675f755aa6536e71611e4 /bash | |
parent | Correct error message (diff) | |
download | dotfiles-553976c1505a391203f078a81b93aa9abb99ebe6.tar.gz dotfiles-553976c1505a391203f078a81b93aa9abb99ebe6.zip |
Use subshell to elegantly preserve shopts
Diffstat (limited to 'bash')
-rw-r--r-- | bash/bashrc.d/cf.bash | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/bash/bashrc.d/cf.bash b/bash/bashrc.d/cf.bash index 032ecc40..db715c2f 100644 --- a/bash/bashrc.d/cf.bash +++ b/bash/bashrc.d/cf.bash @@ -1,7 +1,6 @@ # Count files cf() { - local dir dgs ngs - local -a files + local dir # Specify directory to check dir=${1:-$PWD} @@ -18,27 +17,12 @@ cf() { return 1 fi - # Record current state of dotglob and nullglob - if shopt -pq dotglob ; then - dgs=1 - fi - if shopt -pq nullglob ; then - ngs=1 - fi - - # Retrieve the files array - shopt -s dotglob nullglob - files=("$dir"/*) - - # Reset our options - if ! ((dgs)) ; then - shopt -u dotglob - fi - if ! ((ngs)) ; then - shopt -u nullglob - fi - - # Print result - printf '%d\t%s\n' "${#files[@]}" "$dir" + # Count files and print; use a subshell so options are unaffected + ( + declare -a files + shopt -s dotglob nullglob + files=("$dir"/*) + printf '%d\t%s\n' "${#files[@]}" "$dir" + ) } |