Example it will give you like this
Hey try using basename home/zachary/HW04
Using basename
Using the basename command is the easiest and simplest way to extract the current directory:
basename /usr/local/bin
OutPut will be bin
However, it isn't useful in a shell script with changing directory variables. You can combine it with pwd inside backticks to make it more dynamic:
cd /usr/local/binbasename pwd
bin
Using parameter substitution with echo
The bash scripting language is full of nice tricks, including parameter substitution, which allows you to manipulate or expand variables. You can use parameter substitution with the ${var##pattern} syntax, which removes from $var the longest part of $Pattern that matches the front end of $var. Take a look at an example:
cd /var/log/squidecho ${PWD##*/}
squid
PWD is the environment variable that holds the current path, and ## is the instruction that tells the script to remove everything it finds up to */. In other words, it removes everything until the last /, leaving only the last string, which here is the current directory, squid. You can learn more about parameter substitution and other ways to manipulate variables in the Advanced Bash-Scripting Guide.
Using awk and rev
A more elaborate solution uses a combination of awk (a pattern-scanning utility) and rev (a utility that reverses lines from a file or from stdin):
cd /usr/share/cups/datapwd | rev | awk ââ¬âF / '{print $1}' | rev
data
It's a lot easier to understand this kind of script step by step:
pwd
/usr/share/cups/data
pwd | rev
atad/supc/erahs/rsu/
pwd | rev | awk ââ¬âF / '{print $1}'
atad
pwd | rev | awk ââ¬âF / '{print $1}' | rev
data
The -F option indicates that you should separate by fields, where the field delimiter is /, and that you should print field 1.
Using sed
Finally, you can parse pwd output in the stream editor sed using an elaborate regular expression. This approach may be educational, but it's not practical:
cd /home/smith/musicpwd | sed 's,^(./)\?([^/]),\2,'
music
For a better understanding of how this works, remove the escape character (), which is required for special characters such as "(":
sed 's,^(./)?([^/]),\2,'
pwd. In Unix-like and some other operating systems, the pwd command (print working directory) writes the full pathname of the current working directory to the standard output. The command is a shell builtin in most Unix shells such as Bourne shell, ash, bash, ksh, and zsh.
pwd Display the current working directory. Example: /home/foobar
pwd -P Display the current working directory physical path - without symbolic link name, if any. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/realdir
pwd -L Display the current working directory logical path - with symbolic link name, if any. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/symlinked
https://en.wikipedia.org/wiki/Pwd