Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to evaluate a given path within a bash shell

Is there a bash command that takes as input a file path and returns an absolute file path? More specifically I would like a command that takes as input a path such as:

/tmp/yaneeve/kit/linux/../../output/kit/SOURCES//usr//apps/myapp/lib

and returns the path:

/tmp/yaneeve/output/kit/SOURCES/usr/apps/myapp/lib

Thanks!

like image 949
Yaneeve Avatar asked May 27 '09 06:05

Yaneeve


People also ask

How do I check if a path is valid in bash?

In order to check if a directory exists in Bash using shorter forms, specify the “-d” option in brackets and append the command that you want to run if it succeeds. [[ -d <directory> ]] && echo "This directory exists!" [ -d <directory> ] && echo "This directory exists!"

How do I find the path in bash?

In bash, there are a number of ways of retrieving the full address of a script. In particular, we can use realpath, readlink, or even create our custom little script. When we want to know the directory path, we can use the dirname command in our bash script to retrieve our directory path.

How do you eval in bash?

`eval` command is used in bash to execute arguments like a shell command. Arguments are joined in a string and taken as input for the shell command to execute the command. `eval` executes the command in the current shell.

How do you evaluate a command in shell script?

eval is a built-in Linux command which is used to execute arguments as a shell command. It combines arguments into a single string and uses it as an input to the shell and execute the commands.


1 Answers

If the path exists, there is a portable way which is (even on linux) far more reliable:

canonicalPath=$(cd "$path"; pwd)
like image 181
lhunath Avatar answered Sep 28 '22 02:09

lhunath