${name:h<number>}: Path head

Shortens a file path by dropping trailing segments.

If <number> appears within a parameter expansion (e.g. ${name:h<number>}) and is greater than zero, then this number of leading segments are preserved instead of dropping the last one.

The root path / counts as one segment.

  • $myPath:h drops the trailing segment.
  • $myPath:h2 preserves the first two segments (e.g. /home) and drops all following segments.

Modifier :t is the opposite and evaluates to the tail of the path.

Examples

Print parent directory of a file path
1
2
myPath="/home/user/work/data.csv"
echo $myPath:h
Output:
1
/home/user/work
Convert a relative file path into absolute path and print the directory name
1
2
3
myPath="../work/data.csv"
cd /home/user
echo $myPath:a:h
Output:
1
/home/work
BashSupport Pro ist eine Zsh IDE mit Unterstützung für $varName:h - probieren Sie es jetzt aus!
© 2020–2024 Joachim Ansorg
Impressum
Datenschutz
Rechtliches