Last active
March 8, 2025 00:03
-
-
Save black-dragon74/19694648c1c2dc781fea27449247eb9c to your computer and use it in GitHub Desktop.
A neat script to print the shortpath of current working directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# A nifty utility that prints the PWD in a shortened format. | |
# Place this file in your PATH by removing the .sh extension | |
# If path is: /Our/Custom/path/to/dir | |
# It will output: /O/C/p/t/dir | |
# For directories inside $HOME, ~ is used. | |
path="${1:-$(</dev/stdin)}" | |
home="$HOME" | |
# If the path is inside $HOME, replace it with ~ | |
if [[ "$path" == "$home"* ]]; then | |
path="~${path#$home}" | |
fi | |
# If the path is just `~`, use the username instead | |
if [[ "$path" == "~" ]]; then | |
path="$USER" | |
fi | |
echo "$path" | awk -F'/' '{for (i=2; i<NF; i++) $i = substr($i, 1, 1)} 1' OFS='/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is no longer maintained, this script is now a part of my dotfiles repo. Refer here for the up-to-date version of this script.