Hasty Briefsbeta

More shell tricks: first class lists and jq

17 days ago
  • #jq
  • #shell-scripting
  • #command-line
  • Shells lack first-class lists, making it challenging to return lists from functions.
  • A practical example involves splitting arguments before and after '--' in command-line utilities.
  • jq is highlighted as a powerful tool for manipulating structured data and generating shell-compatible output.
  • The article demonstrates using jq to safely eval dynamic lists in shell scripts, including nested lists.
  • A solution for splitting arguments by '--' using jq is provided, showcasing how to process and return two separate lists.
  • The es shell is introduced as an alternative with first-class functions and structured returns, allowing for list emulation via closures.
  • A method to emulate lists in es using closures is explained, along with a function to split arguments by '--'.
  • The article concludes by acknowledging the challenges and quirks of shell scripting while appreciating its flexibility and the creative solutions it allows.