summaryrefslogtreecommitdiff
path: root/bash_completion
diff options
context:
space:
mode:
authorFreddy Vulto <fvulto@gmail.com>2009-12-02 22:38:30 +0100
committerFreddy Vulto <fvulto@gmail.com>2009-12-02 22:38:30 +0100
commiteb860b7b9f88f0a42bccfc66ab72133cca7bac9f (patch)
treeaadbc7558c5ca6635123f6d637eeb29a250abb66 /bash_completion
parent048e27bf283e08be6762fe2090be224fc6c98fc3 (diff)
downloadbash-completion-eb860b7b9f88f0a42bccfc66ab72133cca7bac9f.tar.gz
Added helper function __expand_tilde_by_ref()
Expands only tilde (~), if first char, in variable. This function displays bash's capabilities of passing a variable by reference (variable indirection) which allows us to avoid using a subshell. As far as I can see it works surprisingly well? To run the automated test: ./runUnit __expand_tilde_by_ref.exp Also fixed some testsuite issues regarding list splitting.
Diffstat (limited to 'bash_completion')
-rw-r--r--bash_completion44
1 files changed, 42 insertions, 2 deletions
diff --git a/bash_completion b/bash_completion
index 12a4f3fb..97320109 100644
--- a/bash_completion
+++ b/bash_completion
@@ -557,6 +557,46 @@ _available_interfaces()
COMPREPLY=( $( compgen -W '${COMPREPLY[@]/%[[:punct:]]/}' -- "$cur" ) )
}
+
+# Expand variable starting with tilde (~)
+# Only the first portion of the variable from the tilde up to the first slash
+# (~../) is expanded. The remainder of the variable, containing for example
+# a dollar sign variable ($) or asterisk (*) is not expanded.
+# Example usage:
+#
+# $ v="~"; __expand_tilde v; echo "$v"
+#
+# Example output:
+#
+# v output
+# -------- ----------------
+# ~ /home/user
+# ~foo/bar /home/foo/bar
+# ~foo/$HOME /home/foo/$HOME
+# ~foo/a b /home/foo/a b
+# ~foo/* /home/foo/*
+#
+# @param $1 Name of variable (not the value of the variable) to expand
+__expand_tilde_by_ref() {
+ # Does $1 start with tilde (~)?
+ if [ "${!1:0:1}" = "~" ]; then
+ # Does $1 contain slash (/)?
+ if [ "${!1}" != "${!1//\/}" ]; then
+ # Yes, $1 contains slash;
+ # 1: Remove * including and after first slash (/), i.e. "~a/b"
+ # becomes "~a". Double quotes allow eval.
+ # 2: Remove * before the first slash (/), i.e. "~a/b"
+ # becomes "b". Single quotes prevent eval.
+ # +-----1----+ +---2----+
+ eval $1="${!1/%\/*}"/'${!1#*/}'
+ else
+ # No, $1 doesn't contain slash
+ eval $1="${!1}"
+ fi
+ fi
+} # __expand_tilde_by_ref()
+
+
# This function expands tildes in pathnames
#
_expand()
@@ -1270,8 +1310,8 @@ _known_hosts_real()
for i in "${tmpkh[@]}"; do
# Remove possible quotes
i=${i//\"}
- # Eval/expand `~' or `~user', only if first char is tilde (~)
- [ "${i:0:1}" = "~" ] && i=$( eval echo "$i" )
+ # Eval/expand possible `~' or `~user'
+ __expand_tilde_by_ref i
[ -r "$i" ] && kh=( "${kh[@]}" "$i" )
done
IFS=$OIFS