How to retrieve absolute path given relative
use:
find "$(pwd)"/ -type f
to get all files or
echo "$(pwd)/$line"
to display full path (if relative path matters to)
Expanding a relative path to a full path when running a C program in shell
Your question is not very well worded so I am not sure I am answering the same question that you are asking, but you can convert "./myScript" to its full path using the realpath() function.
Reliable way for a Bash script to get the full path to itself
Here's what I've come up with (edit: plus some tweaks provided by sfstewman, levigroker, Kyle Strand, and Rob Kennedy), that seems to mostly fit my "better" criteria:
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
That SCRIPTPATH
line seems particularly roundabout, but we need it rather than SCRIPTPATH=`pwd`
in order to properly handle spaces and symlinks.
The inclusion of output redirection (>/dev/null 2>&1
) handles the rare(?) case where cd
might produce output that would interfere with the surrounding $( ... )
capture. (Such as cd
being overridden to also ls
a directory after switching to it.)
Note also that esoteric situations, such as executing a script that isn't coming from a file in an accessible file system at all (which is perfectly possible), is not catered to there (or in any of the other answers I've seen).
The --
after cd
and before "$0"
are in case the directory starts with a -
.
How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?
Use realpath
$ realpath example.txt
/home/username/example.txt
How do I convert relative directory names to absolute ones recognized by Bash?
~/tmp
actually is an absolute path from the bash point of view. However this relies on bash to substitute the ~
to the user accounts home folder path, so to something like /home/users/someone/tmp
which clearly is an absolute path. That is a buildin feature of the bash shell usually called "path expansion". A relative path would be something like ./tmp
or just tmp
, so something that has to be interpreted relative to the current working directory of the process.
That substitution does not get applied here apparently. You can use a slightly altered command to achieve what you are looking for by explicitly forcing such expansion in a sub shell command:
if [ -d `eval echo $CMDLINE_FILENAME` ]; then echo "Valid directory!"; fi
That one works for absolute and relative paths, but also for entries that rely on the bash path expansion:
bash:~$ read CMDLINE_FILENAME
~/tmp
bash:~$ echo "$CMDLINE_FILENAME"
~/tmp
bash:~$ if [ -d `eval echo $CMDLINE_FILENAME`]; then echo "Valid directory!"; fi
Valid directory!
This does carry the risk of miss usage, though: the eval
directive is a pretty mighty tool ...
Related Topics
Platform Independent Resource Management
Can't Run Executable Linked with Libc
Linux/Gcc: Ldd Functionality from Inside a C/C++ Program
How to Open Remotely Installed Sonar on a Browser
How to Check If There Are Symbolic Links Pointing to a Directory
Bash Scripting - How to Set the Group That New Files Will Be Created With
Calculate and Print the Average Value of Strings in a Column
Save and Restore Terminal Content
How to Pass Shell Variables as Command Line Argument to a Shell Script
Find the Ip Address of the Client in an Ssh Session
Symbolic Link: Find All Files That Link to This File
Linux Find File Names with Given String Recursively
How to Build Msi Package on a Linux Server
How to Print a Single Ascii Char
Why Using Pipe for Sort (Linux Command) Is Slow