Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current directory name (without full path) in a Bash script

Tags:

bash

shell

How would I get just the current working directory name in a bash script, or even better, just a terminal command.

pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin

like image 279
Derek Dahmer Avatar asked Sep 03 '09 03:09

Derek Dahmer


People also ask

How do I get the current directory in bash?

By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.

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 the current directory of a script?

pwd can be used to find the current working directory, and dirname to find the directory of a particular file (command that was run, is $0 , so dirname $0 should give you the directory of the current script).

What is basename $0 shell script?

Using the basename Command The $0 is a special variable in bash that represents the filename with a relative path. Hence, we've used the basename command to strip directory names from the script's filename.


2 Answers

Use the basename program. For your case:

% basename "$PWD" bin 
like image 31
Arkady Avatar answered Sep 28 '22 05:09

Arkady


No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:

result=${PWD##*/}          # to assign to a variable  printf '%s\n' "${PWD##*/}" # to print to stdout                            # ...more robust than echo for unusual names                            #    (consider a directory named -e or -n)  printf '%q\n' "${PWD##*/}" # to print to stdout, quoted for use as shell input                            # ...useful to make hidden characters readable. 

Note that if you're applying this technique in other circumstances (not PWD, but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash's extglob support to work even with multiple trailing slashes:

dirname=/path/to/somewhere// shopt -s extglob           # enable +(...) glob syntax result=${dirname%%+(/)}    # trim however many trailing slashes exist result=${result##*/}       # remove everything before the last / that still remains printf '%s\n' "$result" 

Alternatively, without extglob:

dirname="/path/to/somewhere//" result="${dirname%"${dirname##*[!/]}"}" # extglob-free multi-trailing-/ trim result="${result##*/}"                  # remove everything before the last / 
like image 114
Charles Duffy Avatar answered Sep 28 '22 03:09

Charles Duffy