Windows %~dp0 script directory equivalent in Linux
Changes to the directory containing the script.
%~dp0 script directory
cd "$(dirname "$0")"
cd "$(dirname "$0")"
cd "$(dirname "$0")"
Overview
What this command does
On Windows, "%~dp0 script directory" is used to script directory. On Linux, the closest practical mapping shown here is "cd "$(dirname "$0")"". Changes to the directory containing the script. This reference is grouped under Terminal & Shell Scripting → Scripts.
Windows → Linux context
What changes when you use this on Linux?
Equivalent task, different tool
Windows %~dp0 script directory maps to cd "$(dirname "$0")" for this task. Treat this as a workflow mapping rather than a character-for-character syntax translation.
Portable across the compared distros
The Debian/Ubuntu, Fedora and Arch rows use the same command in this mapping. Package availability or defaults can still vary, so check the installed tool version when an option behaves differently.
For the broader concept behind this mapping, read Linux command-line basics.
Practical tasks
Common use cases
Script directory
Changes to the directory containing the script.
cd "$(dirname "$0")"
Script directory
cd --help
Show command help when supported
man cd
Open the manual page when installed
Ready to run
Copyable Linux commands
Review paths, device names, package names and permissions before running any command.
cd "$(dirname "$0")"
Walkthrough
Examples with explanations
# Windows equivalent: %~dp0 script directory
cd "$(dirname "$0")"
Reference
Common options and variations
| Command or option | Use |
|---|---|
cd "$(dirname "$0")" | Script directory |
cd --help | Show command help when supported |
man cd | Open the manual page when installed |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
Debian/Ubuntu, Fedora and Arch all use "cd "$(dirname "$0")"" for this mapping. The command syntax shown here is portable across the three rows, although installed versions and system defaults can still vary.
| Distribution | Package manager / base | Equivalent command | Difference to notice |
|---|---|---|---|
| Debian/Ubuntu | same command | cd "$(dirname "$0")" |
Same command on this distribution. |
| Fedora | same command | cd "$(dirname "$0")" |
Same command on this distribution. |
| Arch | same command | cd "$(dirname "$0")" |
Same command on this distribution. |
Keep learning