How to set current working directory to the directory of the script?

I'm writing a bash script. I need the current working directory to always be the directory that the script is located in.

The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.


#!/bin/bash
cd "$(dirname "$0")"

The following also works:

cd "${0%/*}"

The syntax is thoroughly described in this StackOverflow answer.


Try the following simple one-liners:


For all UNIX/OSX/Linux

dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)

Note: A double dash (--) is used in commands to signify the end of command options, so files containing dashes or other special characters won't break the command.


For Linux, Mac and other *BSD:

cd $(dirname $(realpath $0))

With white spaces support:

cd "$(dirname "$(realpath "$0")")";

Note: realpath should be installed in the most popular Linux distribution by default (like Ubuntu), but in some it can be missing, so you have to install it.

Otherwise you could try something like that (it will use the first existing tool):

cd $(dirname $(readlink -f $0 || realpath $0))

For Linux specific:

cd $(dirname $(readlink -f $0))

Using GNU readlink on *BSD/Mac:

cd $(dirname $(greadlink -f $0))

Note: You need to have coreutils installed (eg 1. Install Homebrew, 2. brew install coreutils ).


In bash

In bash you can use Parameter Expansions to achieve that, like:

cd ${0%/*}

but it doesn't work if the script is run from the same directory.

Alternatively you can define the following function in bash:

realpath () {
  [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}

This function takes 1 argument. If argument has already absolute path, print it as it is, otherwise print $PWD variable + filename argument (without ./ prefix).

or here is the version taken from Debian .bashrc file:

function realpath()
{
    f=$@
    if [ -d "$f" ]; then
        base=""
        dir="$f"
    else
        base="/$(basename "$f")"
        dir=$(dirname "$f")
    fi
    dir=$(cd "$dir" && /bin/pwd)
    echo "$dir$base"
}

Related:

  • How to detect the current directory in which I run my shell script?

  • Getting the source directory of a Bash script from within

  • Bash script absolute path with OSX

  • Reliable way for a bash script to get the full path to itself?

  • See also:

    How can I get the behavior of GNU's readlink -f on a Mac?

    链接地址: http://www.djcxy.com/p/1666.html

    上一篇: Bash脚本绝对路径与OSX

    下一篇: 如何将当前工作目录设置为脚本的目录?