Jimm
Jimm

Reputation: 8505

How to expand relative paths in shell script

I am writing a script to set environment variables on linux 2.6 using bash. So the script contains commands like:

export SRC_DIR=..
export LIBPATH=${SRC_DIR}/lib

the problem is that when i try to do echo $LIBPATH, it shows "../lib" as opposed to expanding the SRC_DIR to full path. I would really like the script to print something like /home/x/lib as opposed to ../lib.

UPDATE The script should evaluate SRC_DIR to be one directory upwards from the script's location and not the current directory from where the script is invoked

Upvotes: 52

Views: 65783

Answers (9)

smac89
smac89

Reputation: 43147

With zsh, it's quite simple using the :P modifier:

export SRC_DIR=..
export LIBPATH=${SRC_DIR:P}/lib

https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers

Upvotes: 2

c00000fd
c00000fd

Reputation: 22283

None of the solutions here worked for me (for zsh). I came up with this:

#!/usr/bin/env bash

your_path="/Users/usr/Documents/Folder/Another/../../../Documents/SomeFile.dmg"

resolved=$(readlink -f "$your_path")
echo "$resolved"

Gives me:

/Users/usr/Documents/SomeFile.dmg

Note that the path in $your_path must exist.

Upvotes: 0

Lerring
Lerring

Reputation: 117

Digging up this old thread to add what i've found to work nicely:

export SRC_DIR = `realpath ..`

see more information here:

http://man7.org/linux/man-pages/man3/realpath.3.html

Upvotes: 10

Zeph
Zeph

Reputation: 9

For keeping error code:

function getPwd() {
    $(cd $1; [[ $? -ne 0 ]] && exit 1 || echo echo $PWD;)
    return $?
}

Upvotes: 0

mbells
mbells

Reputation: 3858

The readlink command is capable of not only resolving symlinks, but also canonicalizing relative paths. Be careful, since you may not want the behaviour of resolving symlinks in all your scripts. If you don't want to resolve symlinks, pwd would be the best; note the use of a subshell, so the cd command does not affect the working directory in the main shell.

# The path you want to get information on... (for readability)
your_path=..

# Using /bin/readlink (resolves symlinks)
export SRC_DIR=$(readlink --canonicalize $your_path)

# Using /usr/bin/dirname (keeps symlinks)
export SRC_DIR=$(cd $your_path ; pwd)

Upvotes: 21

Bernard Edlington
Bernard Edlington

Reputation: 21

When I do this I use echo like so:

export SRC_DIR=`echo $HOME/bin/`

Upvotes: 2

jman
jman

Reputation: 11616

Do this instead:

export SRC_DIR=`pwd`;

Update:

Since you want path relative to the script's location on the filesystem, use this instead:

export SRC_DIR=`dirname $0`

Update2:

Your script must be invoked directly and not as bash /path/to/script.sh or source foo.sh. Add a shebang line, add execute permissions and invoke the script directly.

Upvotes: 0

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84373

Change Directory in Subshell

There's a little trick you can use to get the absolute path from a relative path without changing the present working directory. The trick is to move to the relative path in a subshell, and then expand the working directory. For example:

export SRC_DIR=$(cd ..; pwd)

Relative Paths from Script Instead of Invocation Directory

To change to a relative path from a script's location, rather than the current working directory, you can use a parameter expansion or the dirname utility. I prefer dirname, since it's a little more explicit. Here are both examples.

# Using /usr/bin/dirname.
export SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)

# Using the "remove matching suffix pattern" parameter expansion.
export SRC_DIR=$(cd "${0%/*}/.."; pwd)

Upvotes: 74

choroba
choroba

Reputation: 241938

I usually use

SCRIPT_DIR=$(readlink -f ${0%/*})

It should return the full path to the script, and even resolves all the links along the way.

Upvotes: 12

Related Questions