realpath ~/test
outputs /home/<user>/test
.
Now, I have to use this in my bash script, so I have the path stored in a variable.
If I run the below commands in my shell it works.
export REMOTE_DIR=~/test
realpath $REMOTE_DIR
I have following script:
while true; do
read -p "Enter the path of the Repository : " REMOTE_DIR
if [ -d $(realpath $REMOTE_DIR) ]; then
break;
else
echo "Path not found!! Please enter a valid path."
fi
done
But I get the following error, realpath: '~/test': No such file or directory
.
I observed that the same error occurs when I enclose path in quotes(double or single) as following:
export REMOTE_DIR='~/test'
realpath $REMOTE_DIR
I think the error has to do with something going wrong in the if
statement condition. I guess the $REMOTE_DIR
outputs '~/test'
instead of ~/test
.
What am I doing wrong?
EDIT :
Another issue in the same code: On inputting, ~/test
, Path not found
is output for the following code.
while true; do
read -p "Enter the path of the Repository : " REMOTE_DIR
if [ -d "$REMOTE_DIR" ]; then
break;
else
echo "Path not found!! Please enter a valid path."
fi
done
PS : I'm new to bash scripting!