Inspect GitHub Gists Safely with gh gist view
Someone sends you a gist link and you would rather read it in the terminal than trust a browser tab. gh gist view prints a gist's contents, lists its files, picks one, or hands back raw text for a pipeline. It takes about ten minutes to learn.
The route
Jump straight to the step you need, or tick off Done means at the end.
The examples use GitHub CLI 2.87.3, installed here from the gh package.
- You need: the
ghcommand, network access to GitHub, and permission to read the gist. - No elevated privileges.
- Read-only. This guide never creates, edits or deletes a gist.
1. Check the installed command
Confirm which executable will run and record the local version. These are ordinary read-only commands:
$ command -v gh
/usr/bin/gh
$ gh --version
gh version 2.87.3 (2026-02-23)
https://github.com/cli/cli/releases/tag/v2.87.3
Ask the subcommand for its local contract as well:
$ gh gist view --help
View the given gist or select from recent gists.
USAGE
gh gist view [<id> | <url>] [flags]
Checkpoint
The installed manual documents --filename, --files, --raw and --web. Keep your examples aligned with this output, because options can vary between package versions.
2. View a gist by ID or URL
Pass a gist ID or its URL. Use a real value in place of GIST_ID; the placeholder is deliberately not something to paste unchanged:
$ gh gist view GIST_ID
$ gh gist view https://gist.github.com/USER/GIST_ID
The command renders the gist contents for terminal reading. A successful view normally exits with status 0. The body depends on the gist, so check the status directly when scripting:
$ gh gist view GIST_ID >/tmp/gist-view.txt
$ status=$?
$ printf 'gh gist view exit status: %s\n' "$status"
gh gist view exit status: 0
The temporary file is local output, not a copy written back to GitHub. If the gist holds private or sensitive material, remove the file when you are done:
$ rm -- /tmp/gist-view.txt
Warning
Viewing a private gist still exposes its contents to your terminal, to shell history if you put sensitive values in commands, and to any process or user who can read a file you save. Do not paste secrets into a shared terminal.
3. List the files before choosing one
A gist can contain several files. Ask for names only when you need to discover its shape:
$ gh gist view GIST_ID --files
README.md
script.sh
The names above are illustrative; yours will differ. This option does not print file contents, which makes it a useful checkpoint before you request a large or unexpected file.
Choose the exact name printed by --files and pass it to --filename:
$ gh gist view GIST_ID --filename script.sh
#!/usr/bin/env bash
printf '%s\n' 'gist script'
Quote a filename containing spaces or shell punctuation:
$ gh gist view GIST_ID --filename 'notes for review.txt'
A wrong name gives an error and a non-zero exit status. Do not infer the name from a local file or guess a path; gist filenames come from the gist itself.
4. Get raw content for a pipeline
Use --raw for file content without terminal rendering. Combine it with --filename when the gist has more than one file:
$ gh gist view GIST_ID --filename script.sh --raw
#!/usr/bin/env bash
printf '%s\n' 'gist script'
For a read-only inspection pipeline, send the result to a command that does not execute it:
$ gh gist view GIST_ID --filename script.sh --raw | sed -n '1,20p'
Warning
Do not pipe untrusted gist content to a shell, interpreter or installer. Avoid patterns such as gh gist view ... --raw | sh. Raw output is data, and gh does not make it safe to execute.
5. Open the gist in a browser
Use --web to ask the operating system to open the gist in its browser:
$ gh gist view GIST_ID --web
This needs a usable graphical browser or browser integration in the current environment. It is not a file download and it does not edit the gist.
Tip
On a headless SSH session, use the ID or URL with the terminal options instead.
6. Handle authentication and input errors
A non-interactive shell needs a gist ID or URL. Run the command without one on this machine and you get gist ID or URL required when not running interactively and exit status 1. Give scripts an explicit identifier:
$ gh gist view
gist ID or URL required when not running interactively
$ printf '%s\n' "$?"
1
A private gist may require authentication. Check the CLI session without printing a token:
$ gh auth status
Warning
If the command reports that authentication is required, stop and use your normal approved gh auth login process. Do not put a token in the gist ID, in a shell variable shown in shared logs, or in a command copied into a ticket.
The installed manual gives these exit statuses:
- 0: the command succeeded.
- 1: an error.
- 2: the command was cancelled.
- 4: authentication is required.
Treat any other non-zero result as a failure and keep the diagnostic text for investigation.
Done means
- Version checked. You confirmed the installed
ghversion and local option list. - Explicit viewing. You can view a gist by an explicit ID or URL.
- Files listed first. You can list files before selecting one with
--filename. - Raw used with care. You know when
--rawfits and have not executed untrusted output. - Errors told apart. You can distinguish a missing identifier from an authentication failure.
- Nothing changed. No gist, local service or persistent configuration was modified.