#!/bin/bash value=$(pwd) regex='.*/([a-zA-Z0-9._]*)/([a-zA-Z0-9._]*)$' echo You are here: $value echo The folder is: ${value##*/} [[ $value =~ $regex ]] echo Parent folder: ${BASH_REMATCH[1]}