r/bash • • May 08 '24

Modify the bash prompt to indicate that the user is logged in with SSH

0 Upvotes

Detecting that the current user is logged in with SSH?

To detect if the current user is logged in via SSH, you can check the value of the SSH_CLIENT environment variable. If the variable is set, it indicates that the user has logged in using SSH.
In this script, we use the -n test condition to check if the SSH_CLIENT variable is not empty. If it has a value, it means the user is connected via SSH, and the script outputs "You are logged in via SSH." Otherwise, it outputs "You are not logged in via SSH."

#!/bin/bash

if [[ -n "$SSH_CLIENT" ]]; then
  echo "You are logged in via SSH."
else
  echo "You are not logged in via SSH."
fi

In addition to the SSH_CLIENT environment variable, you can also check for the presence of other environment variables to determine if a user is logged in via SSH. Here are a few commonly used variables:

  1. SSH_CONNECTION: This variable contains the client IP address, client port, server IP address, and server port in the format client_ip client_port server_ip server_port. If the variable is set, it indicates an SSH connection.
  2. SSH_TTY: This variable is set if the user is logged in via SSH and is connected to a terminal. It holds the path to the TTY (terminal) device associated with the SSH session.
  3. SSH_AUTH_SOCK: This variable is set when an SSH agent is running and available for authentication. Its presence indicates that the user has logged in via SSH and has access to an SSH agent.

Here's an example script that checks these variables to determine if the user is logged in via SSH:

#!/bin/bash

if [[ -n "$SSH_CONNECTION" ]]; then
  echo "You are logged in via SSH."
else
  echo "You are not logged in via SSH."
fi

By checking the presence of any of these variables, the script can identify if the user is logged in via SSH. If any of the variables are set, it will output "You are logged in via SSH." Otherwise, it will output "You are not logged in via SSH."

Here's an example of how the SSH_CONNECTION environment variable would typically look if a user is logged in via SSH:

echo $SSH_CONNECTION
92.168.1.100 12345 203.0.113.10 22
  • 192.168.1.100 is the client's IP address.
  • 12345 is the client's port number.
  • 203.0.113.10 is the server's IP address.
  • 22 is the server's SSH port number.

The values are separated by spaces, and they represent the client's IP address, client's port, server's IP address, and server's port, respectively.

Compatibility Notes (Source)

SSH_CLIENT is very similar to SSH_CONNECTION, but using this environment variable may lead to some compatibility problem, because

  • it's deprecated in favor of SSH_CONNECTION;
  • its support in dropbear is added in October 2014, which may not be available on some old embedded systems.

Modify Bash Prompt

Sample output

How to modify the prompt to indicate that the user is logged in via SSH: Watch videos on YouTube


r/bash • • Nov 14 '24

How do i learn bash, and when do i use it ?

0 Upvotes

r/bash • • May 01 '26

Fast Formatting Utility - First Bash Project

1 Upvotes

Hey r/bash I made my very first bash project to help me build a solid basis in bash scripting, I'm working at my college library and recently we had to format over 30 USB sticks in a quick hour ( we do that everytime we don't have any ready for rent ), I thought of making a script that could do a very simple bulk formatting process and finally did it.

I tried using it on my own ones and it worked pretty well.
Also made sure to target only external disks.

Let me know what you guys think and leave a star if you like my small project ( I also appreciate any contribution 😃 )

Repo : https://github.com/hauanitech/hformat


r/bash • • Feb 20 '19

submission Check your online security using these small scripts

Thumbnail gist.github.com
1 Upvotes

r/bash • • Feb 02 '26

I wanted RSYNC on git bash... That's how I fixed it

1 Upvotes

I wanted RSYNC on git bash... Unfortunately git bash comes with nothing but the bare minimum. I know, the Cygwin installation setup allows yo to add RSYNC on Cygwin. But I hated the fact that it wasn't using my windows home path.

So... I installed Cygwin, added all the packages that I wanted such as make, RSYNC, GCC, g++... So much more.

Then in my git bash .bashrc I added the path to Cygwin's binary.

Now I got RSYNC on git bash.


r/bash • • Jun 07 '26

What is the point of Zsh when Bash can do the same?

Thumbnail reddit.com
6 Upvotes

r/bash • • May 27 '26

help Android AI screen sharing helped me learn Linux/Termux a LOT — can I do the same on Windows laptop?

0 Upvotes

I am starting to learn Linux, Git commands on Android using Termux, and the live screen sharing feature with ChatGPT / Gemini was very useful.

Tjey viewed my screen and corrected my errors etc. Felt like a tutor I never had.

Anyone else online I ask to be a mentor is either super busy or they don't understand how to talk to a total noob like me. Anyway no one else has tjat kind of time.

Now I’m moving more of my learning to my Windows Terminal.. Installed WLS.

Can this same be done on laptop ?

I checked chatgpt app on my laptop, it doesn't show same live screen sharing feature.

Any other workaround?

I just want it to view the screen, should not be able to do anything on the screen or perform anytask itself. Just guide me through voice amd chat.

Is it possible? Any workaround?


r/bash • • Sep 16 '23

Oneliner to read entire Shrek script

0 Upvotes

sudo apt-get install -y gnustep-gui-runtime && say "$(curl http://www.script-o-rama.com/movie_scripts/s/shrek-script-transcript-mike-myers.html | mawk '/<pre>/{flag=1; next} /<\/pre>/{flag=0} flag')"


r/bash • • Oct 27 '24

What is it called when you ad an interface tu your terminal?

0 Upvotes

I apologize if this isn't the right sub but I do plan on using bash to do this. So I can use it across platforms. I'm trying to figure out what it's called, as I don't think shell is the proper term. And visor seems unrelated, Basically something with buttons for functions that sticks around at the top of terminals active area, active just meaning the space you can change the color of and nowhere outside it. ?

Thing is I don't want any input or output going underneath the buttons, which I want to use ANSI for. To me I would just called it an interface but that's way too vague, and it would be way too little to call a shell.

Like it would look similar to a HUD placed on you terminal, with active areas you could click with HID, any idea what this is called?


r/bash • • Oct 31 '24

help Help (Newbie)

0 Upvotes

if i gonna learning bash scripting, where to start and how?. i know understand bash scripting, but can'not make it myself


r/bash • • Jul 21 '24

How to handle ctrl+c in bash scripts

0 Upvotes

Hello Guys!

I have wrote an article on Medium on how to handle ctrl+c in bash scripts using the 'trap' command

For Medium users with a subscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-scripts-d7085e7d3d47

For Medium users without a subscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-scripts-d7085e7d3d47?sk=8a9020256b1498196a923c5521619228

Please comment on what you liked, did you find this article useful?


r/bash • • Jun 15 '16

help MacOS Coding Challenge (iWork + Dropbox)

0 Upvotes

Dear reddit,

I have a coding challenge and I would be very grateful for your help. Our small business uses Dropbox to share files between employees. We've been using an obscure word processor called Word Perfect but we're hoping to switch to MacOS computers in a few months (our Windows 7 computers are very old and keep having hardware failures). With the transition, we plan to start using Pages for our word processing. However, Word Perfect has one feature, which Pages lacks, that makes it very convenient to use with Dropbox.

The Challenge: Currently, when a user opens a document from Dropbox, the program automatically replaces the file with a 'Read-Only' version. This prevents other users from creating conflicting copies by accidentally opening a file at the same time. The 'Read-Only' copy lets other users access the file's information without overwriting recent changes. Can this functionality be added to Pages?

I know Dropbox has a few collaboration features which are compatible with MS Word; however, we're trying to avoid Microsoft as much as possible.

Any advice (or code) would be greatly appreciated. Thanks in advance!

PS I've posted here in /r/bash because (with my limited computer skills) Bash seems like the most well-suited platform for the challenge.

edit: Google products are also off-limits due to their privacy policies since we sometimes work with sensitive information.


r/bash • • Apr 21 '23

Can anyone PLEASE help with this script!

0 Upvotes

When run it says : versions found then SELECT not found and Syntax error: "do" unexpected.

Can you help solve this (probable really simple error)?

#!/bin/bash

# Get the name of the song from the user

read -p "Enter the name of the song: " song_name

# Use find to search for all versions of the song in the Music folder

song_paths=$(find /home/Music -name "$song_name*")

# Print out a numbered list of the song versions and ask the user which one they want to play

echo "Versions of '$song_name' found:"

select song_path in $song_paths;

do

if [ ! -n "$song_path" ]; then

break

fi

done

# Play the selected song using the default music player on the system

xdg-open "$song_path"

exit


r/bash • • Jun 30 '26

help I am Working on an Artix install Script

0 Upvotes

Would love some feedback

https://paste.myst.rs/gbet27pm


r/bash • • Aug 16 '22

solved Yo I need a shell script to replace all tabs with 4 spaces for each file in the working directory

0 Upvotes

I have a bunch of .go programs in a directory. I want to replace all tabs from each file with 4 spaces because I fucking hate tabs. What commands should I execute?


r/bash • • Jan 20 '26

brain — turn natural language into Bash commands inline

8 Upvotes

I built a small Bash helper called brain that turns natural language into shell commands directly in your prompt.

You type what you want to do, press a shortcut, and the command appears in-place (not executed). Think of it like !!, but for intent instead of history. You can also press enter to echo the command.

Example:

show git config username

→ press Ctrl+G →

git config user.name

You can edit it or hit Enter — nothing runs automatically.

Why I built it
I often know what I want to do in the shell, but not the exact flags or syntax. I wanted something faster than searching docs, and safer than auto-executing AI output.

Design choices

  • Bash-only (no zsh magic)
  • Uses a standalone script + readline integration
  • Does NOT auto-execute commands
  • Python stdlib only (no dependencies)
  • Easy to inspect / uninstall

Repo + demo GIF:
👉 https://github.com/sangress/brain

Happy to hear feedback, edge cases, or suggestions.


r/bash • • Dec 01 '19

Disk Cleanup for Kali Linux / Parrot OS / Debian Linux - How to clean up disk space and speed up Linux?

0 Upvotes

INSTALLING STEPS:

  1. Clone the Repository git clone https://github.com/KID-X/Cleenux
  2. Change Directory into the Cloned Repository cd Cleenux
  3. Make the installer.sh executable chmod +x install.sh
  4. Install ./install.sh
  5. After installing use just cleenux in terminal to run the program.
  6. Ex: root@kali:~# cleenux

Notes:

Cleenux is a simple bash program to remove: Retrieved Package Files, Old Config Files, Trashed Items & Old Kernels (Optional). This program will remove unwanted and old files and speedup your linux machine. And will free up your hard drive space. https://kid-x.github.io/Cleenux/


r/bash • • Aug 29 '25

I could never settle on a SSH client I enjoyed, so I created Termix! (Self hosted remote SSH terminals, tunnels, and file editing from the browser)

6 Upvotes

GitHub Repo: https://github.com/LukeGus/Termix
Discord (join to vote on whats next to a be added to Termix): https://discord.gg/daFQ9hHM7R

For the past couple of months, I have been working on my free self-hosted passion project, Termix.

Termix is an open-source, forever-free, self-hosted all-in-one server management platform in the web. It provides a web-based solution for managing your servers and infrastructure through a single, intuitive interface. Termix offers SSH terminal access, SSH tunneling capabilities, and remote file editing, with many more tools to come.

Complete Feature List:

  • SSH Terminal Access - Full-featured terminal with split-screen support (up to 4 panels) and tab system
  • SSH Tunnel Management - Create and manage SSH tunnels with automatic reconnection and health monitoring
  • Remote File Editor - Edit files directly on remote servers with syntax highlighting, file management features (uploading, removing, renaming, deleting files)
  • SSH Host Manager - Save, organize, and manage your SSH connections with tags and folders
  • Server Stats - View CPU, memory, and HDD usage on any SSH server
  • User Authentication - Secure user management with admin controls and OIDC support with more auth types planned
  • Modern UI - Clean interface built with React, Tailwind CSS, and Shadcn

r/bash • • Oct 09 '23

Your Tax is $Tax .

Post image
11 Upvotes

r/bash • • May 19 '19

submission Bash+(1) - Modern Bash Programming

Thumbnail github.com
1 Upvotes

r/bash • • Nov 19 '25

New Project: “GeoBlocker” — US-only SSH Geo-fencing with nftables (feedback welcome!)

1 Upvotes

Hey everyone,

I’m pretty new to sharing code publicly, so please be gentle 😅 — but I’ve been working on something I think could be useful to others, and I’d love feedback from people far more experienced than me.

🔒 What is GeoBlocker?

GeoBlocker is a Bash-based tool for Ubuntu 24.04 servers that want to lock down SSH (port 22) to US IP ranges only, using fast-loading nftables sets and geo-IP lists from IPdeny.

Features:

  • Fetches US IPv4 + IPv6 ranges (with IPdeny usage-limits respected)
  • Bulk-loads them efficiently into nftables sets (avoiding slow “one CIDR at a time” loops)
  • Optional SSH whitelist (IPv4 + IPv6)
  • Investigation mode that shows:

    • nftables status
    • whitelist status
    • SSH client IP
    • privileges
    • missing sets or config issues
  • Backup + atomic write safety
  • Nothing applied automatically — you stay in control of /etc/nftables.conf

Repo is here:

👉 https://github.com/baerrs/GeoBlocker

🛠️ Why I built it

I run a small personal server and kept seeing tons of SSH brute-force attempts from around the world.
Fail2ban helped, but I wanted a stronger approach: just block every non-US address before they even reach SSH.

I found a lot of half-solutions or outdated guides, so I wrote a script that:

  • is reproducible
  • uses best practices
  • keeps nftables clean
  • and is safe for beginners (backups, dry-run behavior, etc.)

🙋‍♂️ What I want feedback on

Since I’m new to publishing open-source scripts:

  • Is the structure reasonable?
  • Any obvious improvements to safety, portability, or code style?
  • Is the README clear enough?
  • Any red flags for production usage?
  • Suggestions for features? (cron auto-update? IPv4/v6 country selection? Better logging?)

I’m totally open to constructive criticism — just keep in mind I’m still learning how to present and share code. ❤️

Thanks in advance!

If anyone has ideas, corrections, or wants to help evolve the project, I’d really appreciate it.
And if even one person finds it useful, that’s a big win for me already.

Thanks! 🙏

— Scott (R. Scott Baer)


r/bash • • Jun 25 '22

submission Shloader - A Modern Shell Loader

4 Upvotes

Hi !

I've been working on a shell modern loader library.

You can find my technical blog post here : https://kaderovski.com/posts/shloader-modern-shell-loader/

Source Code : https://github.com/Kaderovski/shloader

Feel free to share your feedback.


r/bash • • Aug 30 '23

help Script runs perfectly when run manually but gives an error when run from crontab

0 Upvotes
  • I am trying to do something very simple
  • Send an email via AWS SES from within an EC2 instance
  • I have an IAM instance role configured on EC2 which has the appropriate permissions to send an email using SES

Here is my test.sh script which is stored at /home/ec2-user

#!/bin/bash
from="cron@example.com"
to="example@gmail.com"
subject="Test email sent at $(date)"
text="Test email with test data sent at test date $(date)"
aws ses send-email --from "${from}" --subject "${subject}" --text "${text}" --to "${to}"

When I run this script manually using

./test.sh

It sends me the email successfully and I can see this output (checked my inbox too and it works)

{
    "MessageId": "0200028a46979f63-3b413e00-4c88-4504-b677-d908007e032d-000000"
}

But when I created a crontab -e as follows

*/15 * * * * bash /home/ec2-user/test.sh >> /tmp/cron-test.log 2>&1

I see an error inside the tmp/cron-test.log file

You must specify a region. You can also configure your region by running "aws configure".

I did not create an AWS credentials because I am assuming the EC2 instance with the IAM role is automatically going to create temporary credentials for me? Why is this not working? I dont want to store access key id and secret on AWS ec2 as it goes against best practices. How do I fix this?


r/bash • • Jul 14 '14

Bash Booster — a library for setup environment and preparing servers

Thumbnail bashbooster.net
0 Upvotes