Bash
in Cheatsheet 🗒 on Bash Last modified at:
- Extract a substring from a string
- Extract a value from a file
- Read a JSON stream
- Tools for Shell Script Development ⚙️
- General 📚
- Features 🔎
- Coding Guidelines, Style, Linter ✍️
- Sponsorship
The only way to really learn scripting is to write scripts – Mendel Cooper
Writing shell scripts leaves a lot of room to make mistakes, in ways that will cause your scripts to break on certain input, or (if some input is untrusted) open up security vulnerabilities… The simplest step is to avoid using shell at all – MIT Student Information Processing Board
Extract a substring from a string
With grep -Eo
and a regex pattern
grep -Eo "http[^ ']+" <<EOF
curl 'https://cloud.sdu.dk/api/ingresses/browse?itemsPerPage=100&includeOthers=true&sortDirection=ascending'
-H 'Accept-Language: en-US,en;q=0.5'
-H 'Accept-Encoding: gzip, deflate, br'
-H 'Authorization: Bearer ****'
-H 'Content-Type: application/json; charset=utf-8'
-H 'Cookie: refreshToken=cf6220bd%2D650c%2D4a48%2D9836%2D58ea9ecb7567'
-H 'Sec-Fetch-Dest: empty'
-H 'Sec-Fetch-Mode: cors'
-H 'Sec-Fetch-Site: same-origin'
-H 'Pragma: no-cache'
-H 'Cache-Control: no-cache'
EOF
will output
https://cloud.sdu.dk/api/ingresses/browse?itemsPerPage=100&includeOthers=true&sortDirection=ascending
src 👉 stackoverflow.com
Extract a value from a file
Example of a pyproject.toml file
[tool.poetry]
name = "docs"
version = "0.1.0"
description = "Collection of Cheatsheets 🗒️ PostgreSQL 🐘, Git..."
repository = "https://github.com/JV-conseil/docs"
With grep
and sed
grep -Eo "^repository.+$" pyproject.toml | sed -E 's/^repository = "(.+)"$/\1/'
will output
https://github.com/JV-conseil/docs
Read a JSON stream
With jq
curl 'https://cloud.sdu.dk/api/ingresses/browse?itemsPerPage=100&includeOthers=true&sortDirection=ascending' \
-H 'Accept-Language: en-US,en;q=0.5' \
-H 'Accept-Encoding: gzip, deflate, br' -H 'Authorization: Bearer *****' \
-H 'Content-Type: application/json; charset=utf-8' \
--compressed | jq '.items[].specification.domain' >~/Downloads/example.json
will output
"app-9a7f8023b8b09392140f3ff9f12c91f2.cloud.sdu.dk"
"app-githubbing.cloud.sdu.dk"
"app-health-check.cloud.sdu.dk"
"app-health-status.cloud.sdu.dk"
"app-mission-ocean.cloud.sdu.dk"
"app-research-funding.cloud.sdu.dk"
"app-santa-maria-josefina-do-coracao-de-jesus-sancho-de-guerra.cloud.sdu.dk"
"app-thalassa.cloud.sdu.dk"
"app-yerun.cloud.sdu.dk"
src 👉 jq/manual
Tools for Shell Script Development ⚙️
ShellCheck Integrates ShellCheck into VS Code, a linter for Shell scripts |
|
---|---|
shell-format A formatter for shell scripts implementing shfmt parser, formatter, and interpreter See Google Shell Style Guide |
General 📚
- Bash Reference Manual - The definitive reference on shell behavior
- Advanced Bash-Scripting Guide - An in-depth exploration of the art of shell scripting by Mendel Cooper
- Awesome Bash - A curated list of delightful Bash scripts and resources
- Shell Scripting for Beginners - How to Write Bash Scripts in Linux
Features 🔎
- The Ultimate Guide to Modularizing Bash Script Code by Shinichi Okada
- How to use a key-value dictionary in Bash
- Bash Parameter expansions
Coding Guidelines, Style, Linter ✍️
- Google Shell Style Guide revision 2.02
- The Unofficial Bash Strict Mode - These lines deliberately cause your script to fail. Wait, what? Believe me, this is a good thing…
- Writing Safe Shell Scripts - MIT Student Information Processing Board
Sponsorship
If this project helps you, you can offer me a cup of coffee ☕️ :-)