Unable to test whether a file exists in Bash

1k views Asked by At

Consider the following Bash script:

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

dosomething

I always get "skipped" although I know that ~/.bash_profile exists. Why?

1

There are 1 answers

2
John Kugelman On BEST ANSWER

~ is only expanded by the shell if it's unquoted. When it's quoted it's a literal tilde symbol.

local fname=~/.bash_profile