${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.
Links
Examples
- Print parent directory of a file path
- Output:
1 2
myPath="/home/user/work/data.csv" echo $myPath:h
1
/home/user/work
- Convert a relative file path into absolute path and print the directory name
- Output:
1 2 3
myPath="../work/data.csv" cd /home/user echo $myPath:a:h
1
/home/work
BashSupport Pro 是一款支持 h** 的 **Zsh 集成开发环境 - 立即试用!