Why the following script does not work:

#!/bin/bash mkdir -p ~/test1 MY_DYR='~/test1' if [ -d $MY_DYR ]; then echo 'TRUE' fi echo 'Done!' 

Expected to see TRUE ...

    1 answer 1

    Because it is part of the rules of the ~ expression: the expression ~ does not work if it is enclosed in quotes.


    Solution 1

    Do not enclose the path with ~ in quotes.

     file=~/path/to/file 

    If you still need to screen part of the path, then do this:

     file=~/"path with spaces/to/file" 

    Solution 2

    Use $HOME instead of ~ .

     file="$HOME/path/to/file" 

    A source