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'
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'
You can use a combination of pwd and basename. E.g.
#!/bin/bash
CURRENT=`pwd`
BASENAME=`basename $CURRENT`
echo $BASENAME
exit;
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:
${PWD##*/}