テーマ: shell
-
grep and ripgrep for code search: recursion, filters and exit codes
GNU grep searches recursively with -r, narrows with --include and --exclude-dir and exits 0, 1 or 2 for match, no match or error; ripgrep does the same by default while skipping git-ignored, hidden and binary files. Know what each tool skips before trusting an empty result.
-
Locale and date pitfalls in coreutils: LC_ALL, character ranges and GNU versus BSD date
The locale silently changes sort order, what [a-z] matches, decimal separators and the default date format; scripts should set LC_ALL=C (or C.UTF-8) and TZ explicitly, request explicit date formats, and avoid GNU-only date -d or BSD-only date -v unless the platform is known.
-
sort, uniq and comm: set operations on text that depend on sorted input
uniq only collapses adjacent duplicates, comm requires both inputs sorted in the same collation, and sort's order follows LC_COLLATE; run such pipelines under LC_ALL=C, pick the right key options (-n, -h, -V, -k, -t) and use comm -12 and comm -23 for intersections and differences.
-
find and xargs without surprises: null-separated names, -exec + and dry runs
File names may contain spaces and newlines, so pass them from find to other programs with -print0 and xargs -0 (or find -exec ... {} +), test the selection with -print before adding -delete, and use xargs -r so that an empty list runs nothing.
-
Working with JSON on the command line with jq
jq reads JSON, applies a filter and prints the result: use -r for plain strings, -c for one object per line, --arg and --argjson to pass values in safely, -e to turn null or false into an exit code, and select, map and to_entries to reshape; never assemble filters by string concatenation.
-
Writing shell scripts that fail safely
Use set -euo pipefail, quote every expansion, prefer [[ ]] and arrays, check tools with ShellCheck, and avoid parsing ls; a script that stops on the first error is easier to trust than one that continues.
-
A day-one setup script that verifies itself: bootstrap, doctor, smoke test
One command after cloning either produces a working environment or a precise list of what is missing and how to fix it: a read-only doctor script checks every requirement, an idempotent bootstrap installs only what the checks report missing, and a smoke test decides the exit status; CI runs it from a clean machine so it cannot rot between newcomers.
-
sed and awk for line-oriented edits: substitution, fields and in-place changes
sed applies editing commands, chiefly s/regexp/replacement/, to each line; awk splits lines into fields and runs pattern-action pairs. Use sed for substitutions and deletions, awk for column work and small aggregations, and handle -i, -n and GNU-versus-BSD differences deliberately.
機械可読: JSON