How to get the current path of a . sh script?

Asked

Viewed 3,166 times

5

On Linux-based systems whenever I need to use the following command:

#!/bin/bash

BASEDIR=$(dirname "$0")

echo $BASEDIR

But I read in different places that $(dirname "$0") not supported by Mac OS X and BSD-based systems (apparently both based on Unix).

I saw some alternatives, but all always have a criticism, or some mention saying that fails in something.

What I need is to know if there is any way to get the current script directory cross-platfom (Unix-like)?

Another thing, I would like to know how to get both the actual directory path and the path of a symbolic link.

Note: Doesn’t have to be with bash

  • Ah, okay, kkkkk, I wavered in the answer

  • the pwd does not suit your case? http://devblog.drall.com.br/get/

  • @Pablovargas thank you, you will tell me if this is cross? However as soon as I install BSD and linux I will test it too. Thank you for now.

  • I can’t tell you, I’ve always used these commands only on Linux

  • pwd is POSIX standard, which means it is highly compatible not only on UNIX-based operating systems (Linux, Android, iOS, Openbsd, macOS, HP-UX, Solaris, ...) but also on Windows 10 through Bash or earlier versions installing SUA(official MS)/Cygwin

2 answers

4

Using the command line in Linux, it is possible to use the pwd shell command to get the path(path) of the target directory. However, when this command is executed inside a directory that is a symbolic link, this command will normally return the path to the symbolic link and not the actual directory pointed out. This behavior is correct and is what is expected. However, sometimes we need to get the actual directory pointed. For this, it is enough:

$ pwd -P 

(shows the path of the real directory pointed by the symbolic link when inside the directory)

$ pwd 

(shows the path of the symbolic link when inside the directory)

  • Great Pablo, I will try to install freebsd and Debian on some virtual machines and test, thanks for now :D

  • 1

    +1, A nice addition would be to put the BASEDIR=$(pwd) in the answer, for users who do not know the shell well know how to capture the value (and mention about the need or not of quotation marks around the ="$(pwd)" )

  • @Bacco is that my answer was more to present the command pwd, I’m not much of a bash connoisseur, but feel free to put in another response with your suggestions.

3

It is strange that the dirname is not available on BSD and in the MacOS, as it is a simple program in C, whose code is in several repositories.

For example: here Opensource Apple and here Openbsd. Just compile and use.

Anyway, it may be useful a solution based on the path relative to the call position, since it allows to locate "always" the script:

Here’s a proposal to try and see if it comes in handy:

script: onde_estou.sh

#!/bin/bash

DIR_RELATIVO="${0%/*}"

DIR_CHAMADA="${PWD}"

SCRIPT_PATH=$DIR_CHAMADA/$DIR_RELATIVO

echo "Desde a posição de chamada:"
echo "--> $DIR_RELATIVO"

echo "Posição de chamada:"
echo "-->$DIR_CHAMADA"

echo "Junção das duas:"
echo "--> $SCRIPT_PATH"

echo " Exemplo de aplicação"
echo "Listar o conteúdo do diretório onde está o script"
ls $SCRIPT_PATH

The script can be called from several different locations and always gives a way there. Unfortunately it makes some curves :).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.