bash - 无法测试文件是否存在于 Bash 中

标签 bash tilde-expansion

<分区>

考虑以下 Bash 脚本:

function dosomething {
    local fname="~/.bash_profile"
    if [[ -f "$fname" ]]; then
        echo "proceeding"
    else
        echo "skipping"
    fi
}

dosomething

尽管我知道 ~/.bash_profile 存在,但我总是被“跳过”。为什么?

最佳答案

~ 只有在未加引号的情况下才会被 shell 扩展。当它被引用时,它是一个字面波浪符号。

local fname=~/.bash_profile

关于bash - 无法测试文件是否存在于 Bash 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52519705/

相关文章:

bash - 等待进程结束或用户输入继续

linux - 带 cron 的 Gsettings

Bash波浪号扩展

Haskell 获取特定用户的主目录路径

bash - 当我登录 GUI 时,波浪号扩展不起作用

arrays - 将 grep 输出存储在数组中

regex - PHP运行多实例问题

linux - Meteor for Linux 使用 Windows 10 Linux bash shell

bash - 在波浪号之前扩展变量

r - 波浪号 "~"表达式在 "R"中不起作用?