How do you handle fail cases in bash?
I'm writing an entire automation framework for cyber security workflows and was curious is it better to create multiple small bash files for each operation, or just use one giant bash noscript.
Also I'm still new to bash and was wondering how you handle stuff that say my noscript has been running for 8 hours and it breaks half way through is there now try or catch we can do to handle this? Now I wasted 8 hours and have to fix and run again?
Also having to re-run my entire noscript again for 8 hours to see if it works is a nightmare 😭
https://redd.it/1q4f39o
@r_bash
I'm writing an entire automation framework for cyber security workflows and was curious is it better to create multiple small bash files for each operation, or just use one giant bash noscript.
Also I'm still new to bash and was wondering how you handle stuff that say my noscript has been running for 8 hours and it breaks half way through is there now try or catch we can do to handle this? Now I wasted 8 hours and have to fix and run again?
Also having to re-run my entire noscript again for 8 hours to see if it works is a nightmare 😭
https://redd.it/1q4f39o
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
[Project] 3STP - A zero-dependency Benchmark tool in pure Bash (supports StdDev, Calibration, and Arg Arrays)
https://github.com/FrancescoTerrosi/3stp/tree/trunk
https://redd.it/1q4k3dk
@r_bash
https://github.com/FrancescoTerrosi/3stp/tree/trunk
https://redd.it/1q4k3dk
@r_bash
GitHub
GitHub - FrancescoTerrosi/3stp
Contribute to FrancescoTerrosi/3stp development by creating an account on GitHub.
Get the last Monday of the week
Hello, I'm writing a bash noscript, and I need to get the last Monday of the week. I used the command "date -d "last Monday" +"%d %b"", but the problem is that yesterday it correctly displayed December 29th, and it's doing the same today, whereas I want it to display today's Monday. Do I need to modify the command, and if so, how? Or should I use an "if" statement so that if today isn't Monday, it displays the last Monday, otherwise it displays this Monday? I hope I've worded my question clearly. Thank you for your help.
https://redd.it/1q4tnaw
@r_bash
Hello, I'm writing a bash noscript, and I need to get the last Monday of the week. I used the command "date -d "last Monday" +"%d %b"", but the problem is that yesterday it correctly displayed December 29th, and it's doing the same today, whereas I want it to display today's Monday. Do I need to modify the command, and if so, how? Or should I use an "if" statement so that if today isn't Monday, it displays the last Monday, otherwise it displays this Monday? I hope I've worded my question clearly. Thank you for your help.
https://redd.it/1q4tnaw
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Looking for a Linux & Unix Discord Community?
Hey everyone,
I don't want to waste your time, so I'll keep this short.
If you like Unix and tech and you want a place where you can ask questions, share what you are working on, or just talk to other enthusiasts as yourself, we have a Discord server called Unixverse.
The server has been active since 2023. We are around 800 members and still growing.
We have dedicated channels for most Unix and Linux distributions, plus general spaces for troubleshooting, tools, and broader tech discussions.
If that sounds like your kind of community, feel free to drop in and have a look.
Server invite link: https://discord.gg/unixverse
Backup invite link: https://discord.gg/rjqgaSHWhd
https://redd.it/1q4w67j
@r_bash
Hey everyone,
I don't want to waste your time, so I'll keep this short.
If you like Unix and tech and you want a place where you can ask questions, share what you are working on, or just talk to other enthusiasts as yourself, we have a Discord server called Unixverse.
The server has been active since 2023. We are around 800 members and still growing.
We have dedicated channels for most Unix and Linux distributions, plus general spaces for troubleshooting, tools, and broader tech discussions.
If that sounds like your kind of community, feel free to drop in and have a look.
Server invite link: https://discord.gg/unixverse
Backup invite link: https://discord.gg/rjqgaSHWhd
https://redd.it/1q4w67j
@r_bash
Discord
Join the Unixverse Discord Server!
Connect with Linux & Unix enthusiasts. Discuss, learn, and share experiences in our vibrant server. 🐧 | 1191 members
Bash is loading super slow with starship installed
Recently I installed starship to give my bash terminal a little bit of flair but after installing brew my load times are over 2 seconds. Is there any way I can track whats making my bash slower? What are some suggestions you guys have?
[EDIT\]: So I fixed the issue, turns out I had an echo command that added 115 lines of on eval command for homebrew EVERY TIME I opened a terminal lmao
https://redd.it/1q72bf3
@r_bash
Recently I installed starship to give my bash terminal a little bit of flair but after installing brew my load times are over 2 seconds. Is there any way I can track whats making my bash slower? What are some suggestions you guys have?
[EDIT\]: So I fixed the issue, turns out I had an echo command that added 115 lines of on eval command for homebrew EVERY TIME I opened a terminal lmao
https://redd.it/1q72bf3
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Run some set of commands after a process ends
Looking for help on a noscript to take as argument a set of commands to run after a process ends.
I often do backups to external HDDs and because they are of different models, they also have different spin-down times. Once backups finish, I want to do different things after, like different the disk, snapshot it, shutdown the drive, move some files after, etc.
For example:
So a while loop to poll for
https://redd.it/1q6r4e5
@r_bash
Looking for help on a noscript to take as argument a set of commands to run after a process ends.
I often do backups to external HDDs and because they are of different models, they also have different spin-down times. Once backups finish, I want to do different things after, like different the disk, snapshot it, shutdown the drive, move some files after, etc.
For example:
./noscript 3872 "sudo defragment /media/hdd; sudo shutdown hdd" where 3872 is the PID of the rsync command. I could do rsync ... ; sudo defragment /media/hdd; ... but sometimes it's more versatile to pass the PID instead of chaining the commands ahead of time (for example, I might want to change the subsequent commands in the chain but if chaining them ahead of time, it requires cancelling the process and starting a new one).So a while loop to poll for
pidof 3872 seems straightforward, but how to best pass an arbitrary set of arguments as an argument to the noscript? pid=$1 ; shift; while pidof "$pid"; do sleep 1; done; "$@"? Are there other things to consider or there a better approach to queuing commands for different processes?https://redd.it/1q6r4e5
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Script, software detection
Script, Software Detection
Hello, how do I write a noscript in bash that triggers an event when a program is launched? I made an example noscript to illustrate what I'm talking about. But of course, the reason I'm here is because the noscript doesn't work properly at all, but it's to illustrate the idea. I'm asking what the correct way to do it is.
While true; do
If pidof -x program > /dev/null ; then
echo "program launched "
exit
fi
sleep 1
donne
https://redd.it/1q5ts5u
@r_bash
Script, Software Detection
Hello, how do I write a noscript in bash that triggers an event when a program is launched? I made an example noscript to illustrate what I'm talking about. But of course, the reason I'm here is because the noscript doesn't work properly at all, but it's to illustrate the idea. I'm asking what the correct way to do it is.
While true; do
If pidof -x program > /dev/null ; then
echo "program launched "
exit
fi
sleep 1
donne
https://redd.it/1q5ts5u
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Good jumping off point for shell noscripting? Looking for a tutorial or class.
I am not new to Linux, but I am a bit new to writing my own shell noscripts. I went through the Learn Linux TV tutorial. It was fun, but I am looking for something a bit more robust, with possibly some projects. Does anyone have any suggestions?
I've asked in a couple other forums and the answers were basically "Find something you want to automate and write a noscript for it." Yes... that's definitely my goal here, but I need a tiny bit more structure than that for my first few steps. I was wondering if there were any tried and true Youtube tutorials or something?
https://redd.it/1q8llzr
@r_bash
I am not new to Linux, but I am a bit new to writing my own shell noscripts. I went through the Learn Linux TV tutorial. It was fun, but I am looking for something a bit more robust, with possibly some projects. Does anyone have any suggestions?
I've asked in a couple other forums and the answers were basically "Find something you want to automate and write a noscript for it." Yes... that's definitely my goal here, but I need a tiny bit more structure than that for my first few steps. I was wondering if there were any tried and true Youtube tutorials or something?
https://redd.it/1q8llzr
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Quick and dirty noscript to generate denoscriptions of all programs in /usr/bin
I used ChatGPT to generate the original noscript, then modified it a bit after testing. It did exactly what I wanted it to do and I thought it was worth sharing.
Edit: formatted the code for readability
#!/bin/bash
# Written by ChatGPT, modified by Jeremy Thurman 1/9/2026
# Original prompt:
# "How would I write a bash noscript to list all the programs in /usr/bin and generate
# a brief denoscription of what they do?"
# A quick and dirty noscript to print a short denoscription of what every program in
# /usr/bin does.
# I remember installing a bunch of command line utilities, but I
# forgot what half of them even were or did. Or I remember installing programs to do
# some things, but forgot what they were called. This noscript goes along way
# towards solving both problems.
BIN_DIR="/usr/bin"
OUTPUT="program_denoscriptions.txt"
> "$OUTPUT"
for cmd in "$BIN_DIR"/*; do
# Only executables, not directories
[[ -x "$cmd" && ! -d "$cmd" ]] || continue
name=$(basename "$cmd")
# Try whatis first
desc=$(whatis "$name" 2>/dev/null | sed 's/^.* - //')
# Fallback to man -f if whatis doesn't return anything
if [[ -z "$desc" || "$desc" == "$name" ]]; then
desc=$(man -f "$name" 2>/dev/null | sed 's/^.* - //')
fi
# Skip it if no denoscription is available
if [[ -z "$desc" ]]; then
# desc="No denoscription available"
continue
fi
printf "%-30s : %s\n" "$name" "$desc" >> "$OUTPUT"
done
https://redd.it/1q8v9ai
@r_bash
I used ChatGPT to generate the original noscript, then modified it a bit after testing. It did exactly what I wanted it to do and I thought it was worth sharing.
Edit: formatted the code for readability
#!/bin/bash
# Written by ChatGPT, modified by Jeremy Thurman 1/9/2026
# Original prompt:
# "How would I write a bash noscript to list all the programs in /usr/bin and generate
# a brief denoscription of what they do?"
# A quick and dirty noscript to print a short denoscription of what every program in
# /usr/bin does.
# I remember installing a bunch of command line utilities, but I
# forgot what half of them even were or did. Or I remember installing programs to do
# some things, but forgot what they were called. This noscript goes along way
# towards solving both problems.
BIN_DIR="/usr/bin"
OUTPUT="program_denoscriptions.txt"
> "$OUTPUT"
for cmd in "$BIN_DIR"/*; do
# Only executables, not directories
[[ -x "$cmd" && ! -d "$cmd" ]] || continue
name=$(basename "$cmd")
# Try whatis first
desc=$(whatis "$name" 2>/dev/null | sed 's/^.* - //')
# Fallback to man -f if whatis doesn't return anything
if [[ -z "$desc" || "$desc" == "$name" ]]; then
desc=$(man -f "$name" 2>/dev/null | sed 's/^.* - //')
fi
# Skip it if no denoscription is available
if [[ -z "$desc" ]]; then
# desc="No denoscription available"
continue
fi
printf "%-30s : %s\n" "$name" "$desc" >> "$OUTPUT"
done
https://redd.it/1q8v9ai
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Automations
Hey everyone
I'm working on making my Linux laptop very automated
I made a couple of noscripts that watch my files and copy paste their names into a local LLM that uses premade noscripts to move files
İt takes the name and format and now's exactly where everything should go inside each place so not videos go to video more like specific video goes to specific video inside videos
And I want more cool automation ideas like this
https://redd.it/1q91ss9
@r_bash
Hey everyone
I'm working on making my Linux laptop very automated
I made a couple of noscripts that watch my files and copy paste their names into a local LLM that uses premade noscripts to move files
İt takes the name and format and now's exactly where everything should go inside each place so not videos go to video more like specific video goes to specific video inside videos
And I want more cool automation ideas like this
https://redd.it/1q91ss9
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Confused by Globbing + Pattern Matching
Hey all,
Apologies if this isn't Bash-specific enough.
It seems like every time I'm writing basic regular expressions or globbing, I've got to re-learn the rules.
This is true of Bash versus ZSH (which is what I'll noscript in for CI, versus writing in the terminal); and regular expressions in more typical application development, like with Javanoscript or Go. This is made more confusing when, in the terminal, you layer on tools like FZF or Grep, which have their own problems (Linux vs. Mac compatibility and POSIX-compliant patterns, etc).
How do you all keep straight which rules apply? I'm having to look up the syntax for basic pattern matching (find me files with this extension, find me files with this prefix, find this substring on this line of text, etc) basically every time. Does this start to stick more with practice? I've been a terminal-based developer for like ~5 years and it's one of those things that I never remember.
Any recommendations on how to make this "stick" when writing noscripts? Do you have any helpful settings to make this simpler?
I feel like there is a constellation of footguns that prevents me from ever learning this stuff.
https://redd.it/1q96may
@r_bash
Hey all,
Apologies if this isn't Bash-specific enough.
It seems like every time I'm writing basic regular expressions or globbing, I've got to re-learn the rules.
This is true of Bash versus ZSH (which is what I'll noscript in for CI, versus writing in the terminal); and regular expressions in more typical application development, like with Javanoscript or Go. This is made more confusing when, in the terminal, you layer on tools like FZF or Grep, which have their own problems (Linux vs. Mac compatibility and POSIX-compliant patterns, etc).
How do you all keep straight which rules apply? I'm having to look up the syntax for basic pattern matching (find me files with this extension, find me files with this prefix, find this substring on this line of text, etc) basically every time. Does this start to stick more with practice? I've been a terminal-based developer for like ~5 years and it's one of those things that I never remember.
Any recommendations on how to make this "stick" when writing noscripts? Do you have any helpful settings to make this simpler?
I feel like there is a constellation of footguns that prevents me from ever learning this stuff.
https://redd.it/1q96may
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
I created SixLogger, a Simple POSIX-compliant Logger function for shell noscripts
https://github.com/esaporski/sixlogger
https://redd.it/1q9g649
@r_bash
https://github.com/esaporski/sixlogger
https://redd.it/1q9g649
@r_bash
GitHub
GitHub - esaporski/sixlogger: A Simple POSIX-compliant Logger function.
A Simple POSIX-compliant Logger function. Contribute to esaporski/sixlogger development by creating an account on GitHub.
My Bash noscript is a bit slow, is there a better way to use 'find' file lists?
EDIT-Solution found at bottom
Script:
#!/usr/bin/bash
rm -fv plot.dat
find . -iname "output.txt" -exec sh -c '
BASE=$(tail -6 < {} | head -n 1 | cut -d " " -f 2)
FAKE=$(tail -3 < {} | head -n 1 | cut -d " " -f 2)
echo $BASE $FAKE >> plot.dat
' {} \;
sort -k1 -n < plot.dat
echo "All done"
The noscript runs on 1,000's of files, and each file has 2k to 10k lines per file. All I really need to do is get the 6th last, and 3rd last lines of the files, and then the 2nd column (always an integer).
I think that tail+head+cut are probably not the issue, I think it might be the creation of thousands of shells through the "find -exec" portion.
Is there a way to get the file list into a variable (an array), and then maybe use a for loop to run the extraction code on the list? The performance isn't important, it still runs in about a minute, this is a question of curiosity about find+shell noscripts.
The bottom of the text files I am parsing look like this:
...
Fake: 34287094987
Fake: 34329141601
Fake: 34349349971
BASE: 1055
Prob Primes: 717268266
Primes: 717267168
Fakes: 1098
Fake %: 0.00015308%
Fake Rate: 1 in 653250
#SOLUTION
This speed-up is really good, from 1m10s to just 10s:
rm -fv plot.dat
for i in /output.txt; do
BASE=$(tail -6 $i | head -n 1 | cut -d " " -f 2)
FAKE=$(tail -3 $i | head -n 1 | cut -d " " -f 2)
echo $BASE $FAKE >> plot.dat
done
https://redd.it/1q9q5pj
@r_bash
EDIT-Solution found at bottom
Script:
#!/usr/bin/bash
rm -fv plot.dat
find . -iname "output.txt" -exec sh -c '
BASE=$(tail -6 < {} | head -n 1 | cut -d " " -f 2)
FAKE=$(tail -3 < {} | head -n 1 | cut -d " " -f 2)
echo $BASE $FAKE >> plot.dat
' {} \;
sort -k1 -n < plot.dat
echo "All done"
The noscript runs on 1,000's of files, and each file has 2k to 10k lines per file. All I really need to do is get the 6th last, and 3rd last lines of the files, and then the 2nd column (always an integer).
I think that tail+head+cut are probably not the issue, I think it might be the creation of thousands of shells through the "find -exec" portion.
Is there a way to get the file list into a variable (an array), and then maybe use a for loop to run the extraction code on the list? The performance isn't important, it still runs in about a minute, this is a question of curiosity about find+shell noscripts.
The bottom of the text files I am parsing look like this:
...
Fake: 34287094987
Fake: 34329141601
Fake: 34349349971
BASE: 1055
Prob Primes: 717268266
Primes: 717267168
Fakes: 1098
Fake %: 0.00015308%
Fake Rate: 1 in 653250
#SOLUTION
This speed-up is really good, from 1m10s to just 10s:
rm -fv plot.dat
for i in /output.txt; do
BASE=$(tail -6 $i | head -n 1 | cut -d " " -f 2)
FAKE=$(tail -3 $i | head -n 1 | cut -d " " -f 2)
echo $BASE $FAKE >> plot.dat
done
https://redd.it/1q9q5pj
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Me desafiei a usar o bash por uma semana.
Fiquei cada vez mais fan do bash por causa desse canal aqui... [yousuckatprogramming\](https://www.youtube.com/@yousuckatprogramming), mas tenho sérios problemas em sair do fish, é muito confortavel então eu vou dar uma chance porque não... como diria um filósofo do meu pais BRIO, quais as principais ferramentas que são uteis para um usário de bash.
https://redd.it/1q9zh1b
@r_bash
Fiquei cada vez mais fan do bash por causa desse canal aqui... [yousuckatprogramming\](https://www.youtube.com/@yousuckatprogramming), mas tenho sérios problemas em sair do fish, é muito confortavel então eu vou dar uma chance porque não... como diria um filósofo do meu pais BRIO, quais as principais ferramentas que são uteis para um usário de bash.
https://redd.it/1q9zh1b
@r_bash
What is /dev/tcp in Linux, and how does it work? Can someone explain it?
https://redd.it/1qa7chp
@r_bash
https://redd.it/1qa7chp
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Need help inserting a variable into a quoted operand
Hi,
convert is an image conversion cli part of the imagemagick suite.
I want to use variables instead of manual entry of text. Example below.
ln1='The first line.' ; convert -pointsize 85 -fill white -draw 'text 40,100 ${ln1}"' -draw 'text 40,200 "The second line."' source.jpg out.jpg
The single quotes confuse me, I've tried different brackets and ways I know of. The help doc about the draw operand offers no examples, or if it's possible.
How can I use the ln1 variable successfully? Thanks.
https://redd.it/1qbhdbk
@r_bash
Hi,
convert is an image conversion cli part of the imagemagick suite.
I want to use variables instead of manual entry of text. Example below.
ln1='The first line.' ; convert -pointsize 85 -fill white -draw 'text 40,100 ${ln1}"' -draw 'text 40,200 "The second line."' source.jpg out.jpg
The single quotes confuse me, I've tried different brackets and ways I know of. The help doc about the draw operand offers no examples, or if it's possible.
How can I use the ln1 variable successfully? Thanks.
https://redd.it/1qbhdbk
@r_bash
Why is the tar --use-compress-program-- here giving an error when extracting the file?
- on running the above noscript i got this error
- why is the --use-compress-program not working when extracting?
- I am on the Apple M1 mac mini with Tahoe 26.1 if that helps
https://redd.it/1qbi0u7
@r_bash
#!/usr/bin/env bash
function create_archive() {
if tar --create --directory="${HOME}/Desktop/noscripts" --file "test.tar.br" --use-compress-program="brotli --quality=6" "test"; then
echo "good"
else
echo "bad"
fi
}
function extract_archive() {
if tar --directory="${HOME}/Desktop/noscripts" --extract --file "test.tar.br" --use-compress-program="brotli --quality=6"; then
echo "good"
else
echo "bad"
fi
}
create_archive
extract_archive
- on running the above noscript i got this error
'/Users/g2g/Desktop/noscripts/tar_functions.sh'
good
tar: Error opening archive: Unrecognized archive format
bad
- why is the --use-compress-program not working when extracting?
- I am on the Apple M1 mac mini with Tahoe 26.1 if that helps
https://redd.it/1qbi0u7
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community