${(D)…}: Reverse filename expansion

(D) assumes that the scalar or array variable contains file paths. It replaces the prefix of these paths with placeholders like ~ and escapes the remainder of the path.

Examples

Print shortened, escaped filepath
1
2
3
4
5
path="$HOME/temp/file with spaces.txt"
echo "File path: ${(D)path}"
  
paths=("$HOME/file1.txt" "$HOME/file2.txt")
echo "File paths: " ${(D)paths}
Output:
1
2
File path: ~/temp/file\ with\ spaces.txt
File paths:  ~/file1.txt ~/file2.txt
BashSupport Pro is a Zsh IDE with support for ${(D)…}try it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal