I want to get the full-path director from a script, even if the user enters the './' or '/~'
#!/bin/bash echo "Enter the directory" read DIRECTORY #how to store the full-path instead of something like ./ echo "$DIRECTORY"
An absolute path begins with
/, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in /*) DIRECTORY="$PWD/$DIRECTORY";; esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
If you want to allow the user to enter a $DIRECTORY that does not exist use:
readlink -m "$DIRECTORY"