Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Passing command arguments using heredoc syntax#
This trick works for both Bash and zsh.
I wanted to pass the following as an argument to the sqlite-utils CLI tool:
1insert into documents select2 substr(s3_ocr_etag, 2, 8) as id,3 key as title,4 key as path,5 replace(s3_ocr_etag, '"', '') as etag6from7 index2.ocr_jobs;Problem: this contains both single AND double quotes, which makes string escaping a tiny bit tricky.
Solution: use heredoc syntax:
1sqlite-utils sfms.db --attach index2 index.db "$(cat <<EOF2insert into documents select3 substr(s3_ocr_etag, 2, 8) as id,4 key as title,5 key as path,6 replace(s3_ocr_etag, '"', '') as etag7from8 index2.ocr_jobs;9EOF10)"Breaking that apart: the main trick here is to use cat <<EOF ... EOF to wrap the literal chunk of text:
1$(cat <<EOF2insert into documents select3 substr(s3_ocr_etag, 2, 8) as id,4 key as title,5 key as path,6 replace(s3_ocr_etag, '"', '') as etag7from8 index2.ocr_jobs;9EOF10)Then to pass it as an argument to the sqlite-utils command use "$(cat ...)" - the double quotes around that ensure that tokens in that input are not treated as separate arguments by zsh/bash.
Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.