help-bash
[Top][All Lists]
Advanced

[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 09:08:12 +0800
User-agent: Mutt/1.5.21 (2010-09-15)

Here it is with the dependency on `awk' removed (no external dependencies):

    tildeExpand() {
        local path=$1

        if [[ $path =~ ^~$ || $path =~ ^~/ ]]; then
            path=${path/\~/$HOME}
        elif [[ $path =~ ^~([[:alnum:]]+) ]]; then
            user=${BASH_REMATCH[1]}
            while IFS=: read -r pUser _ _ _ _ pHome _; do
                if [[ $user == $pUser ]]; then
                    userHome=$pHome
                    break
                fi
            done < /etc/passwd
            if [[ $userHome ]]; then
                path=${path/\~$user/$userHome}
            fi
        fi

        printf '%s\n' "$path"
    }

Again, any suggestions for improvement welcome.

Best,

Chris



reply via email to

[Prev in Thread] Current Thread [Next in Thread]