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

前端 未结 15 2172
离开以前
离开以前 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:56

    I believe this is what you're looking for

    magic() { # returns unexpanded tilde express on invalid user
        local _safe_path; printf -v _safe_path "%q" "$1"
        eval "ln -sf ${_safe_path#\\} /tmp/realpath.$$"
        readlink /tmp/realpath.$$
        rm -f /tmp/realpath.$$
    }
    

    Example usage:

    $ magic ~nobody/would/look/here
    /var/empty/would/look/here
    
    $ magic ~invalid/this/will/not/expand
    ~invalid/this/will/not/expand
    
    0 讨论(0)
  • 2020-11-22 10:00

    I have done this with variable parameter substitution after reading in the path using read -e (among others). So the user can tab-complete the path, and if the user enters a ~ path it gets sorted.

    read -rep "Enter a path:  " -i "${testpath}" testpath 
    testpath="${testpath/#~/${HOME}}" 
    ls -al "${testpath}" 
    

    The added benefit is that if there is no tilde nothing happens to the variable, and if there is a tilde but not in the first position it is also ignored.

    (I include the -i for read since I use this in a loop so the user can fix the path if there is a problem.)

    0 讨论(0)
  • 2020-11-22 10:05

    Due to the nature of StackOverflow, I can't just make this answer unaccepted, but in the intervening 5 years since I posted this there have been far better answers than my admittedly rudimentary and pretty bad answer (I was young, don't kill me).

    The other solutions in this thread are safer and better solutions. Preferably, I'd go with either of these two:

    • Charle's Duffy's solution
    • Håkon Hægland's solution

    Original answer for historic purposes (but please don't use this)

    If I'm not mistaken, "~" will not be expanded by a bash script in that manner because it is treated as a literal string "~". You can force expansion via eval like this.

    #!/bin/bash
    
    homedir=~
    eval homedir=$homedir
    echo $homedir # prints home path
    

    Alternatively, just use ${HOME} if you want the user's home directory.

    0 讨论(0)
提交回复
热议问题