As stated in the title, I need to determine when a program is ran if the path is relative or absolute:
./program #relative dir/dir2/program #relative ~User/dir/dir2/program #absolute /home/User/dir/dir2/program #absolute
This are my test cases. How exactly could I go about doing this in a shell program?
Or more generally, how to check if a path, $0
in this case, is relative or absolute?
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
Firstly, we use the dirname command to find the directory in which a file is located. Then we change the directory using the cd command. Next, we print the current working directory using the pwd command. Here, we have applied the -P option to show the physical location instead of the symbolic link.
if [[ "$0" = /* ]] then : # Absolute path else : # Relative path fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With