r/bash

▲ 5 r/bash

Over ssh, sudo reboot; logout is not always working as expected. Why?

When working over ssh, I sometimes want to reboot or power off a system. The usual error message about the broken pipe annoy me, so I use

sudo reboot; logout as a stop for it. The interesting issue is that it doesn't work on all systems. A sixth gen Intel NUC works, but on an 11th gen Intel CPU I get the same errors as if I didn't issue the logout:

Broadcast message from root@NUC11TNKi3 on pts/1 (Thu 2026-05-21 11:38:21 CEST):

The system will power off now!

Read from remote host nuc11tnki3.lan: Connection reset by peer
Connection to nuc11tnki3.lan closed.
client_loop: send disconnect: Broken pipe

On a 10th gen Intel NUC, it's no issue:

sudo reboot; logout

Broadcast message from root@nuc10i3fnk on pts/2 (Thu 2026-05-21 11:58:15 CEST):

The system will reboot now!

Connection to nuc10i3fnk.lan closed.

It should have something to do with the timing (that's why I post it in /r/bash), but I can't identify why only some systems show it. I use it on some fast systems and on slow systems without problems. Bash version is 5.3.9 in case it plays a role. But I have used other systems with other bash versions as well.

reddit.com
u/spryfigure — 20 hours ago
▲ 2 r/bash

`bind -x '"...": setsid kill -2 $$'` works but `bind -x '"...": kill -2 $$'` does not

I'm stumbling over a behavior in bash that I don't 100% understand. The following doesn't work as expected:

bind -x '"\ex": kill -SIGINT $$'

I would expect this to behave pretty much identical to pressing Ctrl-C, which in a normal terminal in canonical mode would send a SIGINT to the foreground process group of the session associated with the controlling terminal, which is in the case of readline in bash is handled by an appropriate signal handler that aborts the current readline buffer and reprints the prompt. Because of stty echoctl, we should also see a ^C being printed by the terminal itself.

However, this is not what happens. Instead, when I type \ex (Alt-x or Meta-x), it deletes the current PS1 prompt string, does NOT print ^C, moves to a new line, and then prints a new PS1 prompt string. Visually, it looks like the current line was completely erased (basically what printf '\033[2K' would do) and then a new line is created. Running it multiple times creates a lot of empty whitespace. Functionally, it's identical to Ctrl-C, though, which makes sense.

The weird thing is, if I run the kill -SIGINT command from another terminal, OR I adjust the command to be setsid kill instead of just kill (I know that this calls /bin/kill instead of the bash builtin kill, but that's irrelevant to the matter), then it suddenly works exactly as expected, identical to pressing Ctrl-C.

Can someone explain exactly what is going on? Adding stty sane or stty echo echoctl before the kill didn't help, unfortunately. My guess is that in the "bind -x" execution context, the terminal characteristics are set to -echo -echoctl (and maybe some more), but then why doesn't stty sane/stty echo echoctl help it?

u/cubernetes — 20 hours ago
▲ 26 r/bash+1 crossposts

Made a ffmpeg video converter to H.265 script to save space on my video files

Hello, I would love some feedback.
I have made a Bash script using to convert a bunch of .mp4 files to a newer, less space hungry codec (H.265) without a drop in quality.
It only scan for .mp4 files but can be changed quite easily.
After converting, it append "_cc" to the end of the filename, it will also not convert files who already have that substring.
It will delete the original at the end, but can be changed also if needed, then give you info about the total space saved and how long the script was running
https://github.com/PassPhoenix/ffmpeg_converter_H265/blob/main/ffmpeg_h265_converter_mp4.sh
Is there a better way to go about the wall of "echo -n" I did? The code in general?
It's a very simple script and I am learning

Or here for the code:

#! /usr/bin/bash

input_format="mp4"
datasaved=0
SECONDS=0
number_files_converted=0

for file in *."$input_format"; do

  base_name=$(basename "$file" .$input_format)
  output_file="${base_name}_cc.${input_format}"
  
  if [[ $base_name == *"_cc"* ]]; then
    continue
  fi

  size_vid=$(stat --format "%s" "$file")
  echo "Converting file: $file"
  ffmpeg -hide_banner -loglevel error -i "$file" -c:v libx265 -x265-params log-level=none -crf 28 -c:a copy "$output_file" 
  size_vid_after=$(stat --format "%s" "$output_file") 

  echo -n "Converted $file ("
  echo -n "$size_vid"| numfmt --to iec 
  echo -n ") to $output_file ("
  echo -n "$size_vid_after" | numfmt --to iec 
  echo -n ") Reduced by -"
  echo -n $((size_vid - size_vid_after)) | numfmt --to iec
  echo "."
  ((datasaved+= size_vid - size_vid_after))
  echo -n "Size saved so far: "
  echo $datasaved | numfmt --to iec
  
  printf "\n"

  rm "$file"
  ((number_files_converted++))
done

echo -n "Total saved is "
echo $datasaved | numfmt --to iec
duration=$SECONDS
echo "$((duration / 60)) minutes and $((duration % 60)) seconds elapsed for $number_files_converted files converted."
u/Elratum — 2 days ago
▲ 6 r/bash

How to create crontab/cronjob through a script?

I want to add a cronjob entry programmatically through the script instead to manually adding the entry in crontab -e.

Suppose, i have a script that runs to check for ram usage, and I want to add a cronjob inside the same script and run it every 5 mins. Is it possible to do so?

reddit.com
u/JK-Rofling — 3 days ago
▲ 0 r/bash+1 crossposts

Run windows apps on linux?

Im students , need to use MS 365 apps for my education. , and i heard about Winboat .
I dont know actually how to setup it
So what do you think guys it gonna work good or not ? And how can i setup it ( fedora distro )

u/LogicalWrap3405 — 3 days ago
▲ 7 r/bash

Flyline: a Bash plugin to replace readline for a modern line editing experience

Bash is great but I could never configure the command line writing experience to just how I like it.

So I've written a Bash plugin in rust that uses ratatui to provide a modern, smooth command line writing experience. This fills a similar gap to ble.sh but goes beyond what ble.sh offers.

With flyline, you get undo/redo support, tooltips, fuzzy auto completions, fuzzy history search, agent integration, mouse support, text selection, full prompt customization, and more!

And it all runs in the same process as Bash. See the readme on how to install it (no sudo required).

Let me know what you think!

github.com
u/gooddy — 2 days ago
▲ 354 r/bash

Linux basics command lines

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 ?

u/LogicalWrap3405 — 4 days ago
▲ 43 r/bash

Made a shell greeter that generates a unique rocket every time you open a terminal tab

every new tab rolls a random rocket. save the ones you like and they'll come back. ~2×10⁴³ combinations, all deterministic from the hex palette.

rn it works on bash, zsh, powershell, and fish

https://github.com/clefspear/starcommand

lmk what you think!

u/Peetabread8991 — 3 days ago
▲ 2 r/bash+2 crossposts

I built a tool that turns daily hacking practice into a streak habit. Red/Blue/Purple Team paths, real code drills.

hacklingo.tech
u/hacklingo — 3 days ago
▲ 141 r/bash

Started learning Linux from zero , just hit file permissions and my brain is melting (in a good way) lol 🐧

A few weeks ago I didn't know what a terminal was. Now I'm sitting here reading `chmod` output like it's a language I actually understand.

So far I've covered:

- Basic file management commands (`ls`, `cd`, `mkdir`, `rm`, `cp`, `mv`)

- File permissions (`rwx`, owner/group/others, numeric notation)

- `chmod`, `chown`, and how Linux decides who can do what

Anyone else learning Linux from scratch? What topic finally made it all click for you?

u/LogicalWrap3405 — 5 days ago
▲ 12 r/bash

fast alternative to find for finding git directories

Hey,

I have a small script to switch between projects. All my projects are in a deeply nested directory that is equal to their upstream source (eg. ~/projects/github.com/junegunn/fzf/).

It works by using find to enumerate all directories under ~/projects/ that contain a .git/ directory and passes that to fzf. Unfortunately this is pretty slow somehow because findtakes a long time. When using fzf directly it's super fast, but I can't restrict the selection to only include git root directories.

Is there a better way of getting a similar result? All I want is to have a fast way of switching between projects

dev () {
    project="$(find $HOME/projects -type d -name .git -prune -exec sh -c 'dirname $(realpath --relative-to $HOME/projects {})' \; 2>/dev/null | fzf -1)" 
    if [[ $? -ne 0 ]]
    then
        return $?
    fi
    projectDir="$HOME/projects/$project" 
    pushd $projectDir
}
reddit.com
u/chisui — 4 days ago
▲ 43 r/bash

Bash Scripting vs. Python

For those of you who also write scripts in Python or another language besides Bash, How do you decide when to write a script in Python vs. a script in Bash? I'm trying to be economical with my study time, because if I spend a lot of time learning some limited use functionality in one language, I could have used that time to learn a more general use functionality in another language. Here's an example: I've spent a fair amount of time learning awk, but I've never been great at using it, and sometimes I think that I should have just used Path and regex objects in Python, instead.

Edit: Another example is using sed instead of using a regex substitution in python. I've never really gotten comfortable with sed, just like I've never really gotten comfortable with awk--despite spending a fair amount of time trying to learn each.

reddit.com
u/Loud-timetable-5214 — 5 days ago
▲ 12 r/bash+1 crossposts

Monkeypatsh - Make shell monkey patching simple

I got tired of writing wrapper functions by hand or creating aliases that take too much mental space when I just wanted to patch an existing API.

That's why I created Monkeypatsh.

Monkeypatsh is a tool for easily monkey patching commands in the shell:

  • It wraps any command you register with it, npm, git, ls, docker... and lets you attach custom behavior to any existing or new subcommands, flags, or default invocation, while keeping the command's API intact.
  • It centralizes all your patches under one tool and extends the original completion with them.
  • Choose whether these patches stay only in your interactive shell, or are globally available through the $PATH variable.

The gif above shows how easy it is to patch npm run <script> to log the run to a log file.

Would appreciate some feedback. Thanks.

Repo: https://github.com/solisoares/monkeypatsh

u/solisoares — 5 days ago
▲ 23 r/bash

play music using youtube-dl

i made this script that lets you play music directly from YouTube into your terminal using mpv.

give it a try

u/imyatharth — 5 days ago
▲ 11 r/bash+6 crossposts

Bash history not staying after restarts

SOLVED

Solution:

Add these lines to "~/.profile":

if [  "$SHELL" == "/bin/sh" -a -x /bin/bash ]; then
    export SHELL=/bin/bash
    exec /bin/bash --login
fi

if [ "$BASH" ] ; then source ~/.bashrc ; fi

Create file "~/.bashrc" if it doesn't exist

Add these lines to "~/.bashrc":

HISTFILE=~/.bash_history

Run commands:

source ~/.bashrc
source ~/.profile

Restart NAS

==================================

Hello. I'm using a Synology NAS, and I'm trying to make it so my bash commands history stays there even after restarts or updates. Currently, the bash commands history gets cleared when the system restarts.

I read that running "bash --login" can create the ".bashrc" and ".bash_history" files, so I ran it on the NAS via SSH with my user, but it didn't create the files.

When I run "echo $HISTFILE", it returns "/var/tmp/.bash_history", but running "ls -ap /var/tmp/" doesn't show a ".bash_history" anywhere in that directory. Plus, the "/var/tmp/" directory gets cleared everytime the system restarts, so if it actually is there somehow, then it's a bad place for it to be anyway.

I also read that "/etc.defaults/" has a ".bashrc_profile" file that stays there even after the system restarts and updates, although I'm not sure how I'm supposed to use that info at the moment.

How do I create the ".bashrc" and ".bash_history" files, and have them stay on the system after restarting and updating?

reddit.com
u/Alarmed-Prize-7500 — 8 days ago
▲ 6 r/bash+1 crossposts

Sync Wallpaper with Terminator background image?

I was wondering if i could sync the background image of my terminal(Terminator aka x-terminal-emulator) with the current desktop wallpaper and i got to the point of having a bashrc alias that updates the config file's specific line where the background image path resides,but it presents these problems:

  • Manual Input:I must input the alias twice to change the terminator background image to the current desktop wallpaper
  • Turning off and on:The alias also closes and opens new terminator instances,making the split view layout reset everytime i want to manually change it and the change less seamless.

Idea:

  • Theres a specific command to monitor changes in all settings,like window border theme,desktop wallpaper image,desktop wallpaper resize mode,etc etc.Now, could a bg job be searching for any wallpaper changes and act upon that?
  • How would it close and open terminator?Is there a way to avoid this?
reddit.com
u/CrackedTV — 7 days ago
▲ 15 r/bash+1 crossposts

Can someone explain the real-world usage of /etc/profile, profile.d, and bashrc?

Can anyone clearly explain the real-world usage of `/etc/profile`, `/etc/profile.d/`, and `/etc/bashrc` in Linux?

I’m getting confused between different explanations from ChatGPT and Gemini regarding login shells, non-login shells, interactive shells, and non-interactive shells.

From what I understood:

* `.bashrc` is mainly for interactive shell settings like aliases, prompts, etc. * `/etc/profile` and `/etc/profile.d/*.sh` are usually used for global environment variables like `PATH`, `JAVA_HOME`, `ORACLE_HOME`, etc.

But here is where I’m confused:

Some explanations say we should configure application paths and environment variables in `/etc/profile.d/` because they are needed system-wide.

But others say non-interactive or non-login shells (like cron jobs or scripts) do not automatically load `/etc/profile` or `.bashrc` at all.

So my doubts are:

  1. What is the actual real-world purpose of: * `/etc/profile` * `/etc/profile.d/` * `/etc/bashrc` * `.bash_profile` * `.bashrc`
  2. In enterprise environments, where should things like: * `PATH` * `JAVA_HOME` * aliases * prompt customization actually be configured?
  3. If cron jobs/scripts/services don’t automatically load these files, then why is `/etc/profile.d/java.sh` considered standard practice for Java setup?
  4. How do Linux admins usually handle environments for: * SSH users * cron jobs * systemd services * automation scripts

I’m trying to understand the practical production usage rather than only textbook explanations.

reddit.com
u/Western_Head_6650 — 9 days ago
▲ 0 r/bash

i made a tool [zed]

zed

what is zed?

zed is a tool where you can do these stuff listed:
1. overwrite, overwrite is basically you enter a whole new text and the program writes the text to the file

2. linewrite, linewrite is basically you select the line in the file, and you enter the text you want to change, and the program changes the file line with the chosen text!

3. read, reads any file you throw at it

4. delete, self explanatory

5. delline, delete a specific line in a specific file

I WANT THE SOURCE CODE NOW:

ok chill, heres the source code link don't worry you can do whatever to it https://gitlab.com/giorgich11/zed/-/blob/main/source.sh?ref_type=heads

oh... i am to lazy i want the compiled version:
sure go to https://gitlab.com/giorgich11/zed/-/raw/main/zed?ref_type=heads

i want to install this:

okay, when you get the binary or just the .sh just do:

chmod +x zed

if you have the binary you can do 'mv zed /usr/bin/zed' requires sudo! if you don't want to just do whatever to it

I DON'T LIKE THIS I HATE IT:

if you hate it, just leave this post alone please, i beg...

u/giorgich11 — 8 days ago
▲ 7 r/bash

Probably unnecessarily scared of wget's spider function

Trying to find a way to have wget check (maybe in the background?) the size of a website before I attempt to archive it. So if I wanted to run wget -m -k https://example-web.site, I want a script that'll guesstimate how much space it'll take. I found this, with this as the main script; dunno why you'd install the script when you can just copy-paste it into an executable file, but maybe I'm missing something.

So I was about to do that, when I came across this:

> ...--spider option i think unexpectedly deletes all files on disk and so my download of many Gigabytes and thousands of files was being accidentally deleted by this feature. so i run the command in a temporary directory to stop this behavior from accidently deleting files.

And now I'm petrified. Please advise!

reddit.com
u/justquestionsbud — 9 days ago
▲ 49 r/bash

I want start using linux , best way to learn bash scripts ?

I’m thinking seriously about switching to Linux. My idea is to first learn Bash scripting, try making some simple scripts, and at the same time start learning more about Linux distros and how the system works.

I feel like learning Bash first could help me understand Linux better instead of just using it casually.

What’s the best way to learn Bash scripting as a beginner?

reddit.com
u/LogicalWrap3405 — 12 days ago