Skip to content

Instantly share code, notes, and snippets.

@black-dragon74
Last active March 8, 2025 00:03
Show Gist options
  • Save black-dragon74/19694648c1c2dc781fea27449247eb9c to your computer and use it in GitHub Desktop.
Save black-dragon74/19694648c1c2dc781fea27449247eb9c to your computer and use it in GitHub Desktop.
A neat script to print the shortpath of current working directory
#!/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='/'
@black-dragon74
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment