[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] No tilde expansion in [[..]] when reading input with rea
From: |
Chris Down |
Subject: |
Re: [Help-bash] No tilde expansion in [[..]] when reading input with read |
Date: |
Mon, 25 Feb 2013 08:37:27 +0800 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
This piqued my interest. Here's a naive parser (only accepts ~ and ~foo style
expansions) that might do what you want:
tildeExpand() {
local path=$1
if [[ $path =~ ^~$ || $path =~ ^~/ ]]; then
path=${path/\~/$HOME}
elif [[ $path =~ ^~([[:alnum:]]+) ]]; then
user=${BASH_REMATCH[1]}
userHome=$(awk -F: -v user="$user" '$1==user { print $6 }'
/etc/passwd)
if [[ $userHome ]]; then
path=${path/\~$user/$userHome}
fi
fi
printf '%s\n' "$path"
}
This comes with the usual caveats of `$(', etc, although I find it highly
unlikely you would have any newlines in a user's home directory path...
Chris