---
type: collection
section: snippets
generated_at: '2026-10-29T00:31:03+01:00'
total_entries: 1
tag_filter: Security
---

# Snippets

## Generate Secure Passwords with pwgen

---
type: snippet
id: nxm6cdx5fqc6q1rpjk02np47
title: 'Generate Secure Passwords with pwgen'
url: 'https://www.jakubpelak.com/snippets/generate-secure-passwords-with-pwgen'
section: snippets
tags:
  - Security
  - Shell
  - macOS
  - CLI
published_at: 2026-10-28
---

Two tiny shell functions around `pwgen` for generating random passwords from the terminal: `pw` for letters and digits, `pwspec` for when a site wants special characters too. Both default to 20 characters.

```bash
brew install pwgen
```

Add these to your `~/.zshrc` (or `~/.bashrc`):

```bash
# Random password: 20 chars from a-z A-Z 0-9; `pw 5` prints five
pw() { pwgen -s -1 20 "${1:-1}"; }
# Same, with special characters mixed in
pwspec() { pwgen -s -y -1 20 "${1:-1}"; }
```

What the flags do:

- `-s` makes passwords fully random. Without it, `pwgen` generates *pronounceable* ones, which are easier to remember but weaker.
- `-y` adds special characters.
- `-1` prints one password per line instead of filling the screen with columns.

Capital letters and numbers are on by default, and every password gets at least one of each.

### Usage

```bash
pw            # one password
pw 5          # five passwords
pwspec        # one password with special characters
pw | pbcopy   # straight to the clipboard
```

