aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/cf.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2014-10-05 12:26:28 +1300
committerTom Ryder <tom@sanctum.geek.nz>2014-10-05 12:26:28 +1300
commit553976c1505a391203f078a81b93aa9abb99ebe6 (patch)
tree98a04ad10b3fae45b89675f755aa6536e71611e4 /bash/bashrc.d/cf.bash
parentCorrect error message (diff)
downloaddotfiles-553976c1505a391203f078a81b93aa9abb99ebe6.tar.gz
dotfiles-553976c1505a391203f078a81b93aa9abb99ebe6.zip
Use subshell to elegantly preserve shopts
Diffstat (limited to 'bash/bashrc.d/cf.bash')
-rw-r--r--bash/bashrc.d/cf.bash32
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"
+ )
}