Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Created October 8, 2025 17:52
Show Gist options
  • Save shanecelis/b4271aa0c6e9c023b3731d3bacc8ed81 to your computer and use it in GitHub Desktop.
Save shanecelis/b4271aa0c6e9c023b3731d3bacc8ed81 to your computer and use it in GitHub Desktop.
Create a markdown block from command output.
#!/bin/bash
# markcmd
#
# Create a markdown block from command output.
#
# $ markcmd echo hi
# ```sh
# $ echo hi
# hi
# ```
function usage() {
cat << 'EOF';
Usage: markcmd [-hcn] -- [command ...]
-h help, show usage (this)
-c copy to clipboard
-n dry run (don't execute command)
Execute a command and collect its output into a `markdown` block.
EOF
}
dry_run=0;
copy=0;
while getopts hnc opt; do
case $opt in
h) usage; exit;;
n) dry_run=1;;
c) copy=1;;
*) echo "error: invalid option given." >&2; usage;;
esac
done
shift $[ OPTIND - 1 ];
if [ $# -eq 0 ]; then
usage >& 2;
exit 2;
fi
function run_block() {
echo '```sh';
echo "$ $*";
if [ $dry_run -eq 1 ]; then
echo "OMITTED (dry-run)";
else
$@;
fi
echo '```';
}
if [ $copy -eq 1 ]; then
run_block $* | tee /dev/stderr | pbcopy;
else
run_block $*;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment