Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Last active March 2, 2016 21:31
Show Gist options
  • Save paxperscientiam/9f47394397b11f2bae02 to your computer and use it in GitHub Desktop.
Save paxperscientiam/9f47394397b11f2bae02 to your computer and use it in GitHub Desktop.
Demonstration of how braces expansion can be used to shorten $PATH string.
# Version 1.0
# Pax Per Scientiam
# Bash "brace expansion" can be used to compactify long lists of paths.
# I've come up with some basic syntax rules to demonstrate this.
# For the simplest directory structure (i.e. /x00{,/x10{,/x20{,/x30}}}, the expansion result is...
# input>
CDPATH=`printf %s\\: /x00{,/x10{,/x20{,/x30}}}` && echo $CDPATH
# output> /x00:/x00/x10:/x00/x10/x20:/x00/x10/x20/x30:
# For a binary branching structure (i.e. /x00{,/x10{,/x20,/x21},/x11{,/x21,/x22}}, the expansion result is....
# input>
CDPATH=`printf %s\\: /x00{,/x10{,/x20,/x21},/x11{,/x21,/x22}}` && echo $CDPATH
# output> /x00:/x00/x10:/x00/x10/x20:/x00/x10/x21:/x00/x11:/x00/x11/x21:/x00/x11x/22:
# These syntax rules can be combined to form path lists that correspond to directory trees of any complexity
# For example, here's mine:
# input>
CDPATH=.:`printf %s\\: ~{,/Desktop{,/The\ Cloud{,/Dropbox{,{/Career,/Research-MSU-II{,/Brown_Dwarf_Project}}}}},/Library{,/TeXShop}}` && echo $CDPATH
# output> .:/Users/cdr35:/Users/cdr35/Desktop:/Users/cdr35/Desktop/The Cloud:/Users/cdr35/Desktop/The Cloud/Dropbox:/Users/cdr35/Desktop/The Cloud/Dropbox/Career:/Users/cdr35/Desktop/The Cloud/Dropbox/Research-MSU-II:/Users/cdr35/Desktop/The Cloud/Dropbox/Research-MSU-II/Brown_Dwarf_Project:/Users/cdr35/Library:/Users/cdr35/Library/TeXShop:
# If I've done the math right, that reduces the CDPATH character count from 337 to 122 -- wow!
# another example
CDPATH=`printf %s\\: ~{,/Desktop{,/The\ Cloud{,{/Dropbox,/Mega,Cubby}}}}`
#becomes...
# output> /Users/cdr35\:/Users/cdr35/Desktop\:/Users/cdr35/Desktop/The Cloud\:/Users/cdr35/Desktop/The Cloud/Dropbox\:/Users/cdr35/Desktop/The Cloud/Mega\:/Users/cdr35/Desktop/The Cloud/Cubby\:/Users/cdr35\:/Users/cdr35/Desktop\:/Users/cdr35/Desktop/The Cloud\:/Users/cdr35/Desktop/The Cloud/Dropbox\:/Users/cdr35/Desktop/The Cloud/Mega\:/Users/cdr35/Desktop/The Cloud/Cubby\:
#
# By studying the syntax, it becomes much easier to store and interpret complex CDPATH strings.
# Local Variables:
# firestarter: "gist -u 9f47394397b11f2bae02 %p"
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment