Category: digital     |    Tags: tip   linux  

zsh: Parameter expansion cheatsheet

Parent directory

echo $PWD
# /home/learning/pf/zsh/zsh2.00/src
echo $PWD:h
# /home/learning/pf/zsh/zsh2.00

Grandparent directry

echo $PWD:h:h
# /home/learning/pf/zsh

Current dir without path

echo $PWD:t
# src

Extracting file name and extension

name=foo.c
echo $name:r
# foo
echo $name:e
# c

Substituting parts of variable

name=foo.c
echo $name:s/foo/bar/ 
# bar.c

Mass renaming of files

ls
# foo.c    foo.h    foo.o    foo.pro
for i in foo.*; mv $i $i:s/foo/bar/
ls
# bar.c    bar.h    bar.o    bar.pro

Uppercasing and Lower casing

xx=Test
echo $xx:u
# TEST
echo $xx:l
# test

Cutting patterns from beginning and end

echo $PWD
# /home/learning/pf/zsh/zsh2.00/src
echo ${PWD%/*}
# /home/learning/pf/zsh/zsh2.00
echo ${PWD%/*/*}
#/home/learning/pf/zsh

name=foo.c
echo ${name%.*}
# foo

echo ${PWD##*/}
# src

name=foo.c
echo ${name#*.}
# c
© 2019 - 2024 · Home ยท Theme Simpleness Powered by Hugo ·