#!/usr/bin/env bash
# escape4sh -- Escape given command-line arguments as a string that
# is safe to use as input for shells, e.g., sh -c ...
#
# > sh -c "$(escape4sh ARG...)"
##
# Author: Jaeho Shin <netj@cs.stanford.edu>
# Created: 2013-01-24
[[ $# -eq 0 ]] || {
# construct a jq expression that applies shell escaping for each given argument
i=0 jqExpr=
for a; do
let ++i
argsExpr+=' --arg a'$i' "$'$i'"'
jqExpr+='($a'$i' | @sh), '
done
jqExpr=${jqExpr%, }
# then run it
eval "jq --null-input --raw-output $argsExpr '[$jqExpr] | join(\" \")'"
}