>_ IzzyLab

Hands-on task

This task is step 2 of the course “Bash: expansions, arrays and quoting”

Filename without extension

medium~5 min

Write a script /root/strip.sh that, from a given path, prints the filename without the directory and without the last extension. For example /tmp/data.csv → data, /a/b/report.final.txt → report.final. A name without an extension is printed as is; the dot at the start of a hidden file's name is not an extension: /home/u/.bashrc → .bashrc.

What you'll practise:
  • bash
  • parameter substitution
  • ${##}
  • ${%}
  • path handling

Topic breakdown

Bash can cut strings right inside parameter substitution, without calling external basename/dirname. The #/## operators strip a match from the start of the string, %/%% — from the end; a single character removes the shortest match, a double one — the longest (this matters for names with several dots). The task is to apply two such cuts to the argument in sequence: first drop the path down to the filename, then the last extension.

What's checked

  1. directory and last extension stripped
  2. names without an extension and hidden files aren't mangled

What it looks like