Home | Markdown | Gemini | snonux.foo
'\ '\ '\ '\ . . |>18>>
\ \ \ \ . ' . |
O>> O>> O>> O>> . 'o |
\ .\. .. .\. .. .\. .. . |
/\ . /\ . /\ . /\ . . |
/ / . / / .'. / / .'. / / .' . |
jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Art by Joan Stark, mod. by Paul Buetow
somecommand \
| tee >(command1) >(command2) \
| command3
printf 'a\nb\n' \
| tee >(sed 's/.*/X:&/; s/$/ :c1/') >(tr a-z A-Z | sed 's/$/ :c2/') \
| sed 's/$/ :c3/'
a :c3 b :c3 A :c2 :c3 B :c2 :c3 X:a :c1 :c3 X:b :c1 :c3
/bin/sh -c 'echo hi | tee >(cat)'
# /bin/sh: 1: Syntax error: "(" unexpected
set -o pipefail printf 'ok\n' | tee >(false) | cat >/dev/null echo $? # 1 because a side branch failed
ssh "$SSH_USER@$SSH_HOST" <<EOF
# Go to the work directory
cd "$WORK_DIR"
# Make a git pull
git pull
# Export environment variables required for the service to run
export AUTH_TOKEN="$APP_AUTH_TOKEN"
# Start the service
docker compose up -d --build
EOF
FOO=bar cat <<'EOF' $FOO is not expanded here EOF
WORK_DIR="/tmp/my work"
ssh host <<EOF
cd $WORK_DIR # may break if unquoted
cd "$WORK_DIR" # safe
EOF
ssh host <<'EOF'
set -euo pipefail
false # causes immediate failure
echo never
EOF
cat <<-EOF > script.sh #!/usr/bin/env bash echo "tab-indented content is dedented" EOF
#!/usr/bin/env bash
set -euo pipefail
super() {
local -r fn=${FUNCNAME[1]}
# Split name on :: and dispatch to base implementation
local -a parts=( ${fn//::/ } )
"${parts[0]}::base::${parts[2]}" "$@"
}
foo::base::greet() { echo "base: $@"; }
foo::german::greet() { super "Guten Tag, $@!"; }
foo::english::greet() { super "Good day, $@!"; }
for lang in german english; do
foo::$lang::greet Paul
done
base: Guten Tag, Paul! base: Good day, Paul!
user_name=paul declare -n ref=user_name echo "$ref" # paul ref=julia echo "$user_name" # julia
paul julia
make_var() {
local idx=$1; shift
local name="slot_$idx"
printf -v "$name" '%s' "$*" # create variable slot_$idx
}
get_var() {
local idx=$1
local -n ref="slot_$idx" # bind ref to slot_$idx
printf '%s\n' "$ref"
}
make_var 7 "seven"
get_var 7
seven
foo() { echo foo; }
function foo { echo foo; }
function foo() { echo foo; }
deploy_check() { test -f deploy.yaml; }
smoke_test() { curl -fsS http://localhost/healthz >/dev/null; }
if deploy_check || smoke_test; then
echo "All good."
else
echo "Something failed." >&2
fi
deploy_check || smoke_test && echo ok || echo fail >&2
cat > /tmp/ctx.txt <<EOF one foo two three bar EOF grep -C1 foo /tmp/ctx.txt
one foo two
mkdir -p /tmp/golf/foo /tmp/golf/src printf 'bar\n' > /tmp/golf/src/a.txt printf 'bar\n' > /tmp/golf/foo/skip.txt grep -R --exclude-dir=foo 'bar' /tmp/golf
/tmp/golf/src/a.txt:bar
printf 'A\nB\nC\n' > /tmp/s.txt sed -e '1iHEAD' -e '3iMID' /tmp/s.txt
HEAD A B MID C
printf 'a b c\nx y z\n' > /tmp/t.txt
cat /tmp/t.txt
echo
awk 'NF{NF-=1};1' /tmp/t.txt
a b c x y z a b x y
find . -type f -name '*.log' -print0 | xargs -0 rm -f
printf 'a\0b c\0' | xargs -0 -I{} printf '<%s>\n' {}
<a> <b c>
cfg=$(<config.ini)
mapfile -t lines < <(grep -v '^#' config.ini)
printf '%s\n' "${lines[@]}"
printf -v msg 'Hello %s, id=%04d' "$USER" 42 echo "$msg"
Hello paul, id=0042
mapfile -d '' -t files < <(find . -type f -print0)
printf '%s\n' "${files[@]}"
LC_ALL=C tr -dc 'A-Za-z0-9_' </dev/urandom | head -c 16; echo
openssl rand -base64 16 | tr -d '\n' | cut -c1-22
yes | rm -r large_directory # auto-confirm yes n | dangerous-command # auto-decline yes anything | head -n1 # prints one line: anything
true() { return 1; }
false() { return 0; }
true || echo 'true failed'
false && echo 'false succeeded'
# Bypass function with builtin/command
builtin true # returns 0
command true # returns 0
rbash -c 'cd /' # cd: restricted rbash -c 'PATH=/tmp' # PATH: restricted rbash -c 'echo hi > out' # redirection: restricted rbash -c '/bin/echo hi' # commands with /: restricted rbash -c 'exec ls' # exec: restricted
# Prefer grep -i foo file <file grep -i foo # or feed via redirection # Over cat file | grep -i foo
cat file1 file2 | grep -i foo
lockdir=/tmp/myjob.lock
if mkdir "$lockdir" 2>/dev/null; then
trap 'rmdir "$lockdir"' EXIT INT TERM
# critical section
do_work
else
echo "Another instance is running" >&2
exit 1
fi
find . -name '*.log' -exec gzip -9 {} +
shopt -s extglob ls -d -- !(.git|node_modules) 2>/dev/null