惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

abyss.fish thoughts

abyss * your_dotfiles_are_not_a_distro abyss * tree-style_invite_systems_reduce_AI_slop abyss * all_eyes_on_minneapolis j3s.sh j3s.sh j3s.sh j3s.sh j3s.sh j3s.sh j3s.sh j3s.sh j3s.sh
j3s.sh
2023-10-06 · via abyss.fish thoughts

↵return

shell tip: print json with printf

another pufferfish drawn by rekka

shell tip: print json with printf 2023-10-06 i am commonly consulted when people are having strange issues with shell. one of the most common issues that shows up is when people try printing their json in shell. printing json might seem straightforward at first, just: $ echo '{"name": "jes", "sign": "aquarius"}' {"name": "jes", "sign": "aquarius"} but inevitably, variables show up: $ echo '{"name": "$name", "sign": "$sign"}' {"name": "$name", "sign": "$sign"} <-- this output is very broken at this point, the shell user realizes that they need double quotes in order to make variables interpolate properly. the user then commits one of two *cardinal* sins: sin 1: the eternal double-quote swamp $ echo "{\"name\": \"$name\", \"sign\": \"aquarius\"}" {"name": "jes", "sign": "aquarius"} if you write anything as ugly as the abomination above in shell, you are sending a clear signal that you do not care about yourself. you are condemning yourself to wade through the double-quote swamp for eternity. and you will deserve it. '\"\"\"\"\"\""\""\"'\\""\"""\\""\""\"\\\"\""\""\"""\\""\\" ^ a small portion of the eternal double-quote swamp ^ sin 2: the long ugly HEREDOC $ cat <<-EOF {"name": "$name", "sign": "$sign"} EOF {"name": "jes", "sign": "aquarius"} the HEREDOC is more forgivable, but it takes up 2 unnecessary lines, and it's much harder to recall on-the-fly. and frankly, it looks pretty strange. and god forbid if you have to use a heredoc in an indented block. kiss your nice orderly indentation goodbye: if blah; then cat <<-EOF {"name": "$name", "sign": "$sign"} EOF fi there is a better way! we just need to bust out our old dusty friend *printf* printf offers several advantages over echo: - many languages (C, Go) have a printf equivalent, so it feels familiar - printf behavior does not differ across systems (it is posix compliant) - printf handles escape sequences (printf "hello\nworld\n") - printf can trivially handle json witness, as we print the same json text using printf: $ printf '{"name": "%s", "sign": "%s"}' "$name" "$sign" {"name": "jes", "sign": "aquarius"} & there you have it! nice orderly json using an easy to remember command, with no-fuss variable injection built-in. i hope that this tip saves you some pain! love, jes

follow me on mastodon or bluesky!

last updated 2023-10-06T00:00:00.000Z