59 minutes ago · Tech · hide · 0 comments

Zsh modifiers are a family of suffixes that transform words as they’re expanded. Several of them are dedicated to slicing up filenames, replacing fiddly calls to dirname, basename, and friends. Let’s take a look at them now! The filename modifiers Four modifiers cover most filename manipulation. Take this variable assignment to a path: $ f=talks/robot.png These modifiers each return a different part of the path: :h (“head”) keeps the directory part, like dirname: $ print ${f:h} talks Like dirname, it returns . when there’s no directory part: $ g=robot.png $ print ${g:h} . :t (“tail”) keeps the filename part, like basename: $ print ${f:t} robot.png :r (“root”) drops the extension: $ print ${f:r} talks/robot :e (“extension”) keeps only the extension: $ print ${f:e} png Modifiers can be chained, applying left to right. For example, :t:r does “tail” then “root”, reducing a path to its bare name: $ print ${f:t:r} robot An example with :r I recently wanted to convert some PNG images into…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.