Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

How may I detect the name of the directory (or better yet the entire path) in which my shell script is run?

like image 493
goe Avatar asked Nov 30 '09 01:11

goe


People also ask

How do I find my current directory name in shell?

pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal. This is a shell building command that is available in most Unix shells such as Bourne shell, ash, bash, kash, and zsh.

How do I get current working directory in Bash?

Print Current Working Directory ( pwd ) To print the name of the current working directory, use the command pwd . As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory.


2 Answers

what shell? What operating system?

For starters try

man pwd
$PWD
like image 68
Anycorn Avatar answered Sep 28 '22 01:09

Anycorn


This, I believe, is the most portable way:

dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
like image 29
glenn jackman Avatar answered Sep 28 '22 02:09

glenn jackman