Bash code examples
13 short, tested snippets. Pick one to see the code with notes, or search Zest for something specific, such as “bash for loop”.
- for loopfor i in 1 2 3; do echo "$i"; done
- if statementif [ -f "$file" ]; then echo "file exists"; fi
- Read a file line by linewhile IFS= read -r line; do
- Find filesfind . -name "*.log" # by name
- grep recursivelygrep -rn "pattern" . # recursive, with line numbers
- curl POST JSONcurl -X POST https://api.example.com/items \
- Create and extract archivestar -czf archive.tar.gz folder/ # create
- Variables and argumentsname="world"
- sed and awk one-linerssed -i 's/old/new/g' file.txt # replace in place
- Generate an SSH keyssh-keygen -t ed25519 -C "you@example.com"
- Sync files with rsyncrsync -avz source/ user@host:/path/dest/ # local to remote
- Redirect output and pipescmd > out.txt # overwrite
- Hello world#!/usr/bin/env bash