In a shell script, a relative path is usually used to do the processing, but sometimes we may need to use an absolute path.
How do I get an absolute path to a file or directory in the shell?
Perhaps you will immediately think of Realpath (if you have written PHP), so the shell supports this command. Take a look here and you'll know.
Realpath appears to come from Debian, and are not part of the Coreutils,realpath appears not to be standard issue.
This command is not always available. We can actually use the Readlink command: readlink-f file_path
file_real_path=$ (readlink-f.) Get the current real path
file_real_path=$ (readlink-f. /..) Get the real path to the first level
So you can get the absolute path of the file, readlink the specific use of the man Readlink.
Readlink for Softlink's processing to get the actual file path, not Softlink path.
A better way to get the path of the currently executing script is:
curdir=$ (CD $ (dirname ${bash_source[0]}); pwd)
Echo $CURDIR
Other common paths are:
Gets the path of the current execution script
filepath=$ (CD "$ (dirname" $) "; pwd)
echo "$ (basename $) $ (dirname $)--$filepath"
The absolute path to the script file exists in the environment variable filepath, which can be used with echo $filepath
$: Gets the name of the current script
$#: Number of parameters passed to script
$$: The process number of the shell script