r/bash • • Apr 17 '25

Is this still valid for you in 2025?

Post image
1.4k Upvotes

When everything else fails, there's always a bash script you forgot you wrote in 2019 that's still holding the infrastructure together.


r/bash • • Feb 23 '26

Stop installing tools just to check if a port is open. Bash has it built in.

1.2k Upvotes

Instead of:

telnet host 443
# or
nmap host -p 443

Just use:

echo > /dev/tcp/host/443 && echo "open" || echo "closed"

No tools required. No sudo. No package manager. Works on any machine with bash.

/dev/tcp is a bash built-in pseudo-device. Bash handles the TCP connection itself — the kernel never sees a file open on /dev/tcp.

Real world examples:

# Check if SSH is up
echo > /dev/tcp/192.168.1.100/22 && echo "SSH up" || echo "SSH down"

# Check if your web server is listening
echo > /dev/tcp/localhost/80 && echo "nginx up" || echo "nginx down"

# Check SSL port before running a cert check
echo > /dev/tcp/example.com/443 && echo "open" || echo "closed"

# Loop until a service comes up (great for scripts)
until echo > /dev/tcp/localhost/5432; do
    echo "waiting for postgres..."
    sleep 2
done

That last one is the killer use case — waiting for a service to become available in a deploy script without installing netcat or curl or anything else.

One caveat: this is bash-specific. Won't work in sh, zsh, or fish. If portability matters, use nc -z host port instead.

Works on Linux and macOS.


r/bash • • Feb 25 '26 Hooray Bravo

tips and tricks Stop retyping long commands just to add sudo

945 Upvotes

You run a long command. It fails with permission denied. So you hit up arrow, go to the beginning of the line, type sudo, and hit enter.

Stop doing that.

sudo !!

!! expands to your last command. Bash runs sudo in front of it. Works with pipes, redirects, everything:

cat /var/log/auth.log | grep sshd | tail -20
# permission denied
sudo !!
# runs: sudo cat /var/log/auth.log | grep sshd | tail -20

Where this actually saves you is commands with flags you don't want to retype:

systemctl restart nginx --now && systemctl status nginx
# permission denied
sudo !!

Works in bash and zsh. Not available in dash or sh.


r/bash • • Jul 01 '26 Bravo

submission Intellisense autocompletions inside of Bash

Thumbnail
gallery
818 Upvotes

The completions are generated using Bash's existing completion framework (commonly scop/bash-completion or your own completion scripts). And if you don't have a completion script setup, flyline will try to synthesize one on the fly (😉) using man pages or --help output!

This is similar to https://github.com/microsoft/inshellisense but inshellisense only works for a hardcoded list of completions specifications and runs in a different process as Bash.

Flyline has a bunch of other features, so if you're interested you can check it out here: https://github.com/HalFrgrd/flyline. Thanks!

This software's code is partially AI-generated


r/bash • • Mar 05 '26

tips and tricks Stop leaving temp files behind when your scripts crash. Bash has a built-in cleanup hook.

731 Upvotes

Instead of:

tmpfile=$(mktemp)
# do stuff with $tmpfile
rm "$tmpfile"
# hope nothing failed before we got here

Just use:

cleanup() { rm -f "$tmpfile"; }
trap cleanup EXIT

tmpfile=$(mktemp)
# do stuff with $tmpfile

trap runs your function no matter how the script exits -- normal, error, Ctrl+C, kill. Your temp files always get cleaned up. No more orphaned junk in /tmp.

Real world:

# Lock file that always gets released
cleanup() { rm -f /var/run/myapp.lock; }
trap cleanup EXIT
touch /var/run/myapp.lock

# SSH tunnel that always gets torn down
cleanup() { kill "$tunnel_pid" 2>/dev/null; }
trap cleanup EXIT
ssh -fN -L 5432:db:5432 jumpbox &
tunnel_pid=$!

# Multiple things to clean up
cleanup() {
    rm -f "$tmpfile" "$pidfile"
    kill "$bg_pid" 2>/dev/null
}
trap cleanup EXIT

The trick is defining trap before creating the resources. If your script dies between mktemp and the rm at the bottom, the file stays. With trap at the top, it never does.

Works in bash, zsh, and POSIX sh. One of the few tricks that's actually portable.


r/bash • • Mar 14 '26 Bravo

tips and tricks Stop passing secrets as command-line arguments. Every user on your box can see them.

727 Upvotes

When you do this:

mysql -u admin -pMyS3cretPass123

Every user on the system sees your password in plain text:

ps aux | grep mysql

This isn't a bug. Unix exposes every process's full command line through /proc/PID/cmdline, readable by any unprivileged user. IT'S NOT A BRIEF FLASH EITHER -- THE PASSWORD SITS THERE FOR THE ENTIRE LIFETIME OF THE PROCESS.

Any user on your box can run this and harvest credentials in real time:

while true; do
    cat /proc/*/cmdline 2>/dev/null | tr '\0' ' ' | grep -i 'password\|secret\|token'
    sleep 0.1
done

That checks every running process 10 times per second. Zero privileges needed.

Same problem with curl:

curl -u admin:password123 https://api.example.com

And docker:

docker run -e DB_PASSWORD=secret myapp

The fix is to pass secrets through stdin, which never hits the process table:

# mysql -- prompt instead of argv
mysql -u admin -p

# curl -- header from stdin
curl -H @- https://api.example.com <<< "Authorization: Bearer $TOKEN"

# curl -- creds from a file
curl --netrc-file /path/to/netrc https://api.example.com

# docker -- env from file, not command line
docker run --env-file .env myapp

# general pattern -- pipe secrets, don't pass them
some_command --password-stdin <<< "$SECRET"

The -p with no argument tells mysql to read the password from the terminal instead of argv. The <<< here string and @- pass data through stdin. Neither shows up in ps or /proc.

Bash and any POSIX shell. This isn't shell-specific -- it's how Unix works.


r/bash • • Mar 03 '25

critique What in god's name is the purpose of this?

Post image
658 Upvotes

r/bash • • Feb 21 '26

tips and tricks Stop typing the filename twice. Brace expansion handles it.

640 Upvotes

Stop typing the filename twice. Brace expansion handles it. Works on any file, any extension.

#Instead of

cp config.yml config.yml.bak

#Do

cp nginx.conf{,.bak}

cp .env{,.bak}

cp Makefile{,.$(date +%F)}

# That last one timestamps your backup automatically. You're welcome.


r/bash • • Feb 08 '26

Read epstein files directly from your terminal via grepstein.sh

592 Upvotes

Hi there,

I recently developed a Bash script that allows you to read Epstein files directly from your terminal because bash manual found in epstein files and i tought that reading the bash manuel that found in epstein files via bash scripting is a cool idea. Isn't it ? 😊

It's a simple one, but building it taught me a lot along the way.

You can check out the repository here: https://github.com/ArcticTerminal/grepstein

I’d really appreciate it if you could take a look at the source code and share your thoughts. I didn’t use AI to write this script, so I’m sure there are areas that could be improved or optimized—any constructive feedback is more than welcome.

Here’s a short demonstration:

https://youtu.be/Bd55Hh53Dms

Thanks!

edit : repo url change.

edit 2 : Script updated for stability, FZF support added for better UI and Dockerfile added for Win/Mac Users.


r/bash • • Mar 01 '26

tips and tricks Stop creating temp files just to compare command output. Bash can diff two commands directly.

587 Upvotes

Instead of:

cmd1 > /tmp/out1
cmd2 > /tmp/out2
diff /tmp/out1 /tmp/out2
rm /tmp/out1 /tmp/out2

Just use:

diff <(cmd1) <(cmd2)

<() is process substitution. Bash runs each command and hands diff a file descriptor with the output. No temp files, no cleanup.

Real world:

# Compare two servers' packages
diff <(ssh server1 'rpm -qa | sort') <(ssh server2 'rpm -qa | sort')

# What changed in your config after an update
diff <(git show HEAD~1:nginx.conf) <(cat /etc/nginx/nginx.conf)

# Compare two API responses
diff <(curl -s api.example.com/v1/users) <(curl -s api.example.com/v2/users)

Works anywhere you'd pass a filename. grep, comm, paste, wc -- all of them accept <().

Bash and zsh. Not POSIX sh.


r/bash • • Mar 10 '26

tips and tricks Stop holding the left arrow key to fix a typo. You've had `fc` the whole time.

569 Upvotes
# you just ran this
aws s3 sync /var/backups/prod s3://my-buket/prod --delete --exclude "*.tmp"
#                                      ^typo

Hold ← for ten seconds. Miss it. Hold again. Fix it. Run it. Wrong bucket. Rage.

Or:

fc

That's it. fc opens your last command in $EDITOR.Navigate directly to the typo, fix it, save and quit — the corrected command executes automatically.

Works in bash and zsh. Has been there since forever. You've just never needed to know the name.

Bonus: fc -l shows your recent history. fc -s old=new does inline substitution without opening an editor. But honestly, just fc alone is the one you'll use every week.


r/bash • • Jun 26 '20

FFS, *stop* deleting your posts when your problem is solved.

557 Upvotes

I've about had it with seeing my carefully considered replies disappear into oblivion as the asker deletes their post.

This is a public subreddit. It's not your commercial helpdesk service. It's not your personal fucking homework tutor. No one is getting paid for posting here.

You're supposed to leave your questions and their answers up, forever, so that others can learn from the answers too, now and in the future. That's how this works, and has done since Usenet in the 1970s.

Stop being such selfish twerps, k? k.


edit: Looks like this has been a problem for a while. Previous discussion. I might have to adopt the technique recommended in that post…


r/bash • • May 18 '26

tips and tricks Linux basics command lines

Post image
550 Upvotes

Here is some basic linux command line .

what do y'all think all is good or i need to add some in file and management ?


r/bash • • Nov 11 '20

lol.sh

Post image
459 Upvotes

r/bash • • Sep 12 '22 Take My Silver

set -x is your friend

456 Upvotes

I enjoy looking through all the posts in this sub, to see the weird shit you guys are trying to do. Also, I think most people are happy to help, if only to flex their knowledge. However, a huge part of programming in general is learning how to troubleshoot something, not just having someone else fix it for you. One of the basic ways to do that in bash is set -x. Not only can this help you figure out what your script is doing and how it's doing it, but in the event that you need help from another person, posting the output can be beneficial to the person attempting to help.

Also, writing scripts in an IDE that supports Bash. syntax highlighting can immediately tell you that you're doing something wrong.

If an IDE isn't an option, https://www.shellcheck.net/

Edit: Thanks to the mods for pinning this!


r/bash • • Feb 22 '26

tips and tricks Stop leaking secrets into your bash history. A leading space handles it.

447 Upvotes

Instead of typing:

export AWS_SECRET=abc123

# now in history forever

Just add a space before the command:

export AWS_SECRET=abc123

curl -H "Authorization: Bearer $TOKEN" 'https://api.example.com'

mysql -u root -pSuperSecret123

None of those will appear in history.

One requirement — add this to your ~/.bashrc or ~/.zshrc if it isn't already set:

HISTCONTROL=ignorespace

Bonus: use ignoreboth to also skip duplicate commands:

HISTCONTROL=ignoreboth

No more scrambling to scrub credentials after accidentally pasting them into the wrong terminal. Works in bash and zsh.


r/bash • • Dec 16 '25

Isn't this the greatest BASH course ever?

447 Upvotes

https://www.youtube.com/watch?v=Sx9zG7wa4FA : YSAP

The way this guy explains concepts with depth and clarity in it is insane. The fact that he self-learnt everything through man pages is something which keeps me driven in tech.


r/bash • • Dec 14 '19

An interactive cheatsheet tool for the command-line

418 Upvotes

r/bash • • Feb 04 '26

The BASH Reference Manual is part of The Epstein Files.

Post image
414 Upvotes

Seriously 👀 The BASH scripting language. In the Epstein Files.
https://www.justice.gov/epstein/files/DataSet%209/EFTA00315849.pdf


r/bash • • Jan 11 '25

Something i do on all BASH scripts I write. What do you guys think?

Post image
405 Upvotes

Something I do to almost every one of my scripts is add the following at the top:

The idea behind this is I can add in debugging i_echo statements along the way throughout all of my code. If i start the script with a -i it turns INTERACT on, and display all of the i_echo messages.

You can easily reverse this by turning INTERACT to true by default if you generally want to see the messages, and still have the -q (quiet) option.

Would anyone else out there find this helpful?


r/bash • • Jan 04 '26

submission My first game is written in Bash

Post image
305 Upvotes

I wanted it to be written in pure Bash but stty was needed to truly turn off echoing of input.

Story time:

I'm fairly new to programming. I do some web dev stuff but decided to learn Bash (and a little bit of C) on the side. I wanted to finish a small project to motivate myself so I made this simple snake game.

During the process, I learned a bit about optimization. At first, I was redrawing the grid on each frame, causing it to lag so bad. I checked the output of bash -x after one second to see what's going on and it was already around 12k lines. I figured I could just store the previous tail position and redraw only the tile at that coordinate. After that and a few more micro-optimizations, the output of bash -x went down to 410 lines.

I know it's not perfect and there's a lot more to improve. But I want to leave it as is right now and come back to it maybe after a year to see how much I've learned.

That's all, thanks for reading:)

EDIT: here's the link: https://github.com/sejjy/snake.sh


r/bash • • May 05 '23

Had a suit made, they asked if I wanted anything embroidered...

Post image
255 Upvotes

This may be the nerdiest thing I've ever done.


r/bash • • Feb 03 '26

tips and tricks guys you should read the bash manual

230 Upvotes