Skip to content

Bash

https://jvns.ca/blog/2022/04/12/a-list-of-new-ish--command-line-tools/

Bash shortcuts

Clipboard

  • pbcopy & pbpaste

https://osxdaily.com/2007/03/05/manipulating-the-clipboard-from-the-command-line/

  • pbcopy < file.txt
  • ps aux | pbcopy

  • pbpaste > pastetest.txt

  • pbpaste | grep rcp

xargs

  • Run a command using the input data as arguments:
  • arguments_source | xargs command

General Tips

Double Dash (--)

  • tells shell to treat any future dashes like strings and not flags
    • npm test -- -u -t="ColorPicker"
    • the -u is a flag for the command and not npm
grep "--hello" data.txt
grep: unrecognized option '--hello'
grep -- --hello data.txt

Why git checkout -- file.txt

  • the -- is optional in this case
  • just a good habit, since branches usually have dashes

Command substitution

result=$(curl -X GET 'URL')

String substitution

${var}

"$var_name"
  • TODO: what's the difference?

Secrets

read -s PGPASSWORD
secretly_type_your_password
export PGPASSWORD

direnv

  • Checks for .envrc > .env
  • makes them environment variables

ripgrep (rg)

  • better than grep
  • ripgrepall also searches PDFs and other files
    • brew install rga
    • brew install pandoc poppler tesseract ffmpeg

Read logs

lnav has syntax highlighting for logs

Hyperfine CLI Benchmarking Performance

Typos

Python list to bash

space_separated_strings=$(python file.py | tr -d '[],')

# We need the eval because ????
eval "for query_content in $query_content_list; do
    echo \$query_content
    psql2 $COMMISSIONS_DB -c \$query_content;
done"

su vs sudo

  • su switches users
  • sudo keeps the current user, runs as if they were sudo
  • su - creates a new environment with the super user's env var and switches to their home directory
  • sudo su -

Open a man file in Preview (as a PDF)

man -t apropos | open -fa Preview


Last update: 2022-09-23