Signal9Signal9 — home
online
back to labs

Parameter Expansion: Strip the Path

MediumCommand Line Guru10-15 min
The brief

Shell parameter expansion can slice a variable without calling basename or dirname. ${VAR##/} strips the longest leading match of /, which removes everything up to and including the last slash and leaves the filename.

Write /root/scripts/filename.sh. It takes a full path as its first argument and prints just the filename, using parameter expansion rather than basename, sed, awk or cut. For example, bash /root/scripts/filename.sh /path/to/document.txt prints document.txt.

Grading runs your script on paths you haven't seen (with dots in the directory names and more than one dot in the filename), so it has to compute the filename, not print a fixed one.

What you'll do
  • Write /root/scripts/filename.sh that prints the filename of the path given as $1
  • Strip the directory with ${VAR##*/}, not basename or another external tool

Hands-on lab: you work on a real Linux box in your browser, graded on the machine's actual state.