How to manually expand a special variable (ex: ~ tilde) in bash

前端 未结 15 2358
离开以前
离开以前 2020-11-22 09:34

I have a variable in my bash script whose value is something like this:

~/a/b/c

Note that it is unexpanded tilde. When I do ls -lt on this

15条回答
  •  爱一瞬间的悲伤
    2020-11-22 09:39

    Just use eval correctly: with validation.

    case $1${1%%/*} in
    ([!~]*|"$1"?*[!-+_.[:alnum:]]*|"") ! :;;
    (*/*)  set "${1%%/*}" "${1#*/}"       ;;
    (*)    set "$1" 
    esac&& eval "printf '%s\n' $1${2+/\"\$2\"}"
    

提交回复
热议问题