Unix - How to get current folder name in shell variable

I need to get current folder or directory name without full path in my Bash script. How can I get current folder name? I tried the pwd command but it gives full path such as /home/user/project. I need only project name from /home/user/project. How can I do it?

Many options.

Use pwd and basename command to get current folder name

basename $(pwd)
mydir="$(basename $PWD)"
echo "$mydir"

The basename command remove directory and suffix from filenames. The pwd display name of current or working directory. The current working directory as set by the cd command stored in $PWD shell variable. So mydir="$(basename $PWD)" I am extracting that current directory using $PWD and removing everything except current folder or directory name using the basename.

Bash parameter substitution

Both basename and pwd are external commands. We can use internal command too to avoid forking:

echo "$current_dir"
printf "%s\n" "${PWD##*/}"
echo "${PWD##*/}"

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki