cat file | head fails, when using "strict mode"
I use "strict mode" since several weeks. Up to now this was a positive experience.
But I do not understand this. It fails if I use `cat`.
```
#!/bin/bash
trap 'echo "ERROR: A command has failed. Exiting the noscript. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0")"; exit 3' ERR
set -Eeuo pipefail
set -x
du -a /etc >/tmp/etc-files 2>/dev/null || true
ls -lh /tmp/etc-files
# works (without cat)
head -n 10 >/tmp/disk-usage-top-10.txt </tmp/etc-files
# fails (with cat)
cat /tmp/etc-files | head -n 10 >/tmp/disk-usage-top-10.txt
echo "done"
```
Can someone explain that?
> GNU bash, Version 5.2.26(1)-release (x86_64-pc-linux-gnu)
https://redd.it/1l8tjbx
@r_bash
I use "strict mode" since several weeks. Up to now this was a positive experience.
But I do not understand this. It fails if I use `cat`.
```
#!/bin/bash
trap 'echo "ERROR: A command has failed. Exiting the noscript. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0")"; exit 3' ERR
set -Eeuo pipefail
set -x
du -a /etc >/tmp/etc-files 2>/dev/null || true
ls -lh /tmp/etc-files
# works (without cat)
head -n 10 >/tmp/disk-usage-top-10.txt </tmp/etc-files
# fails (with cat)
cat /tmp/etc-files | head -n 10 >/tmp/disk-usage-top-10.txt
echo "done"
```
Can someone explain that?
> GNU bash, Version 5.2.26(1)-release (x86_64-pc-linux-gnu)
https://redd.it/1l8tjbx
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Need Help Finding a Specific Config File in Linux
How to Find a Config File Created After 2020-03-03 (Size Between 25k and 28k)
I'm trying to track down a configuration file that meets these criteria:
Created/modified after March 3, 2020
Between 25KB and 28KB in size
Likely has a .conf or .cfg extension
I tried this command:
find / -type f \\( -name "\.conf" -o -name "*.cfg" \\) -size +25k -size -28k -newermt 2020-03-03 2>/dev/null
But I'm not sure if I'm missing anything. Some specific questions:
1. Are there other common locations besides /etc where configs might live?
2. Should I be using -cnewer instead of -newermt?
3. How would you modify this to also check file permissions?
https://redd.it/1l8zckm
@r_bash
How to Find a Config File Created After 2020-03-03 (Size Between 25k and 28k)
I'm trying to track down a configuration file that meets these criteria:
Created/modified after March 3, 2020
Between 25KB and 28KB in size
Likely has a .conf or .cfg extension
I tried this command:
find / -type f \\( -name "\.conf" -o -name "*.cfg" \\) -size +25k -size -28k -newermt 2020-03-03 2>/dev/null
But I'm not sure if I'm missing anything. Some specific questions:
1. Are there other common locations besides /etc where configs might live?
2. Should I be using -cnewer instead of -newermt?
3. How would you modify this to also check file permissions?
https://redd.it/1l8zckm
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Need Help: How to Check Services Listening on All Interfaces (IPv4 Only, Excluding Localhost)?
I’m auditing a system and need to find all services listening on all IPv4 interfaces (excluding
ss -tuln | grep -v "127.0.0.1" | awk '$5 !\~ /:::/ {print $5}' | cut -d: -f2 | sort -u
Questions:
1. Is this accurate?
2. Should I use
3. How to also filter out IPv6 (
Context:
Target: Debian 12 server
Goal: Identify potentially exposed services (e.g., MySQL, Redis) bound to `0.0.0.0` or external IPs.
https://redd.it/1l92p9k
@r_bash
I’m auditing a system and need to find all services listening on all IPv4 interfaces (excluding
localhost/127.0.0.1). Here’s what I’ve tried:ss -tuln | grep -v "127.0.0.1" | awk '$5 !\~ /:::/ {print $5}' | cut -d: -f2 | sort -u
Questions:
1. Is this accurate?
2. Should I use
netstat instead of ss for legacy systems?3. How to also filter out IPv6 (
:::) without complicating the command?Context:
Target: Debian 12 server
Goal: Identify potentially exposed services (e.g., MySQL, Redis) bound to `0.0.0.0` or external IPs.
https://redd.it/1l92p9k
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Bash noscript - How to check string length at specific loop iteration?
I'm working on a noscript that repeatedly base64 encodes a string, and I need to get the character count at a specific iteration. Here's what I have:
**#!/bin/bash**
**var="nef892na9s1p9asn2aJs71nIsm"**
**for counter in {1..40}**
**do**
**var=$(echo $var | base64)**
**# Need to check length when counter=35**
**done**
**What I need:**
When the loop hits iteration 35, I want to print ONLY the length of **$var** at that exact point.
**What I've tried:**
1. **${#var}** gives me length but I'm not sure where to put it
2. **wc -c** counts extra bytes I don't want
3. Adding **if \[ $counter -eq 35 \]; then echo ${#var}; fi** but getting weird results
**Problem:**
* The length output disappears after more encodings
* Newlines might be affecting the count
* Need just the pure number as output
**Question:**
What's the cleanest way to:
1. Check when the loop is at its 35th pass
2. Get the exact character count of **$var** at that moment
3. Output just that number (no extra text or newlines)
https://redd.it/1latikt
@r_bash
I'm working on a noscript that repeatedly base64 encodes a string, and I need to get the character count at a specific iteration. Here's what I have:
**#!/bin/bash**
**var="nef892na9s1p9asn2aJs71nIsm"**
**for counter in {1..40}**
**do**
**var=$(echo $var | base64)**
**# Need to check length when counter=35**
**done**
**What I need:**
When the loop hits iteration 35, I want to print ONLY the length of **$var** at that exact point.
**What I've tried:**
1. **${#var}** gives me length but I'm not sure where to put it
2. **wc -c** counts extra bytes I don't want
3. Adding **if \[ $counter -eq 35 \]; then echo ${#var}; fi** but getting weird results
**Problem:**
* The length output disappears after more encodings
* Newlines might be affecting the count
* Need just the pure number as output
**Question:**
What's the cleanest way to:
1. Check when the loop is at its 35th pass
2. Get the exact character count of **$var** at that moment
3. Output just that number (no extra text or newlines)
https://redd.it/1latikt
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Built a Minimal TCP Port Scanner in C — Learning Networking from Scratch
https://www.reddit.com/gallery/1lbnqvt
https://redd.it/1lbnuss
@r_bash
https://www.reddit.com/gallery/1lbnqvt
https://redd.it/1lbnuss
@r_bash
Reddit
From rootninja07's profile on Reddit: Built a Minimal TCP Port Scanner in C — Learning Networking from Scratch
Explore this post and more from rootninja07's profile
Using cut to get versions
Suppose I have two different styles of version numbers:
- 3.5.2
- 2.45
What is the best way to use cut to support both of those. I'd like to pull these groups:
- 3
- 3.5
- 2
- 2.4
I saw that cut has a delemiter, but I don't see where it can be instructed to just ignore a character such as the period, and only count from the beginning, to however many characters back the two numbers are.
As I sit here messing with cut, I can get it to work for one style of version, but not the other.
https://redd.it/1lbtgyg
@r_bash
Suppose I have two different styles of version numbers:
- 3.5.2
- 2.45
What is the best way to use cut to support both of those. I'd like to pull these groups:
- 3
- 3.5
- 2
- 2.4
I saw that cut has a delemiter, but I don't see where it can be instructed to just ignore a character such as the period, and only count from the beginning, to however many characters back the two numbers are.
As I sit here messing with cut, I can get it to work for one style of version, but not the other.
https://redd.it/1lbtgyg
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
tzview - Display in local time lunchtime in other timezones.
I wrote a shell noscript that displays the current time in various
timezones. It is useful for organizing meetings with people in different
timezones, do not create a meeting at lunchtime to someone in Australia.
https://github.com/harkaitz/sh-tzview
https://redd.it/1ldj1fs
@r_bash
I wrote a shell noscript that displays the current time in various
timezones. It is useful for organizing meetings with people in different
timezones, do not create a meeting at lunchtime to someone in Australia.
https://github.com/harkaitz/sh-tzview
https://redd.it/1ldj1fs
@r_bash
GitHub
GitHub - harkaitz/sh-tzview: Display in local time the moment in time in other timezones.
Display in local time the moment in time in other timezones. - harkaitz/sh-tzview
Rewriting a utility function noscripts library for Linux
I've made a simple utility functions noscripts librarytsilvs.bashlib for Bash.
Daily-driving Bazzite, I've designed it to simplify some interactions with Fedora Silverblue family of distros, especially
I'd like to reduce the amount of repetative code. If you have some time, review my code please. Re-implementation suggestions are welcome too.
tsilvs.bashlib: https://github.com/tsilvs/bashlib
https://redd.it/1ldnpge
@r_bash
I've made a simple utility functions noscripts librarytsilvs.bashlib for Bash.
Daily-driving Bazzite, I've designed it to simplify some interactions with Fedora Silverblue family of distros, especially
rpm-ostree. But it might come in handy for active ADB and Git users too.I'd like to reduce the amount of repetative code. If you have some time, review my code please. Re-implementation suggestions are welcome too.
tsilvs.bashlib: https://github.com/tsilvs/bashlib
https://redd.it/1ldnpge
@r_bash
Bash-based command-line tool to compare two folders and create html reports
https://redd.it/1le397d
@r_bash
https://redd.it/1le397d
@r_bash
nn - minimalist note taking tool for CLI using Zettelkasten in bash
https://github.com/joaocgduarte/nn
https://redd.it/1le09hz
@r_bash
https://github.com/joaocgduarte/nn
https://redd.it/1le09hz
@r_bash
GitHub
GitHub - joaocgduarte/nn
Contribute to joaocgduarte/nn development by creating an account on GitHub.
Script to see how much space each incremental backup uses - may be useful
I've a noscript that uses rsync to create incremental backups, and I wanted have a list of the directories and the amount of space each backup is using. Here it is:
https://github.com/funkytwig/funkierbackup/blob/main/dir\_usage.bash
The output looks something like this:
https://redd.it/1ldns6p
@r_bash
I've a noscript that uses rsync to create incremental backups, and I wanted have a list of the directories and the amount of space each backup is using. Here it is:
https://github.com/funkytwig/funkierbackup/blob/main/dir\_usage.bash
The output looks something like this:
/home/ben/test_backup/2025/06/15/23_D: 384KB/home/ben/test_backup/2025/06/16/14_H: 128KB/home/ben/test_backup/2025/06/16/15_H: 132KB/home/ben/test_backup/2025/06/16/16_H: 120KB/home/ben/test_backup/2025/06/16/17_H: 128KB/home/ben/test_backup/2025/06/16/18_H: 120KB/home/ben/test_backup/2025/06/16/19_H: 120KB/home/ben/test_backup/2025/06/16/20_H: 120KB/home/ben/test_backup/2025/06/16/21_H: 136KB/home/ben/test_backup/2025/06/16/22_H: 128KB/home/ben/test_backup/2025/06/16/23_D: 124KB/home/ben/test_backup/2025/06/17/00_H: 120KB/home/ben/test_backup/2025/06/17/01_H: 120KB/home/ben/test_backup/2025/06/17/02_H: 120KB/home/ben/test_backup/2025/06/17/03_H: 120KB/home/ben/test_backup/2025/06/17/04_H: 120KB/home/ben/test_backup/2025/06/17/05_H: 120KB/home/ben/test_backup/2025/06/17/06_H: 120KB/home/ben/test_backup/2025/06/17/07_H: 120KB/home/ben/test_backup/2025/06/17/08_H: 120KB/home/ben/test_backup/2025/06/17/09_H: 120KB/home/ben/test_backup/2025/06/17/10_H: 120KB/home/ben/test_backup/2025/06/17/11_H: 120KB/home/ben/test_backup/2025/06/17/12_H: 120KB/home/ben/test_backup/2025/06/17/13_H: 120KB/home/ben/test_backup/2025/06/17/14_H: 184KBhttps://redd.it/1ldns6p
@r_bash
GitHub
funkierbackup/dir_usage.bash at main · funkytwig/funkierbackup
Contribute to funkytwig/funkierbackup development by creating an account on GitHub.
Found a Bash-based uptime monitor… it’s just curl in a while loop
Discovered our “external uptime check” was literally -
Running on a Raspberry Pi under someone’s desk, with no logging, no alerting, and no supervision.
Dropped it into Blackbox hoping for some clever logic. Nope. Just curl.
Anyone else stumbled across “creative” Bash in prod like this?
https://redd.it/1lejy8s
@r_bash
Discovered our “external uptime check” was literally -
while true; do
curl $SERVICE_URL
sleep 60
doneRunning on a Raspberry Pi under someone’s desk, with no logging, no alerting, and no supervision.
Dropped it into Blackbox hoping for some clever logic. Nope. Just curl.
Anyone else stumbled across “creative” Bash in prod like this?
https://redd.it/1lejy8s
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Go-like programming language that transpiles down to Batch or Bash
Hey Bash enthusiasts!
A while ago I wanted to get a bit into compiler/transpiler building and first I couldn't really think about something useful. So I thought, which language is super complicated to use even for the most basic tasks? And than it hit me...Batch! So that's what my small Go-like language became, a Batch transpiler, but it can also transpile to Bash (that's why I also posted it here).
Give it a try, I would like to hear your thoughts on it :)
https://github.com/monstermichl/TypeShell
https://redd.it/1lev9bi
@r_bash
Hey Bash enthusiasts!
A while ago I wanted to get a bit into compiler/transpiler building and first I couldn't really think about something useful. So I thought, which language is super complicated to use even for the most basic tasks? And than it hit me...Batch! So that's what my small Go-like language became, a Batch transpiler, but it can also transpile to Bash (that's why I also posted it here).
Give it a try, I would like to hear your thoughts on it :)
https://github.com/monstermichl/TypeShell
https://redd.it/1lev9bi
@r_bash
GitHub
GitHub - monstermichl/TypeShell: Go-like programming language that transpiles down to Batch or Bash
Go-like programming language that transpiles down to Batch or Bash - monstermichl/TypeShell
Help understanding ambiguous redirection behavior in bash
I'm working on building my own small shell that mimics bash behavior, and I'm trying to understand when and why "ambiguous redirect" errors happen.
Consider this situation:
Now these two examples behave differently:
I'm confused — why does using
Also, I noticed that in some cases,
But in this case, it doesn’t seem to:
Can someone explain when
Thanks in advance!
https://redd.it/1lf6ccw
@r_bash
I'm working on building my own small shell that mimics bash behavior, and I'm trying to understand when and why "ambiguous redirect" errors happen.
Consider this situation:
export a=" " // just a bunch of spacesNow these two examples behave differently:
ok$a"hhhhh"$.... // this is NOT ambiguous -works fineok$a"hhhhh"$USER // this IS ambiguousI'm confused — why does using
$a (which is just spaces) before a variable like $USER lead to an ambiguous redirect, but using it before a string of characters like ... doesn’t?Also, I noticed that in some cases,
$a splits the word:ok$a"hhh"$USER # gets split due to spaces in $aBut in this case, it doesn’t seem to:
ok hhhhh$... # stays as one word?Can someone explain when
$a (or any variable with spaces) causes splitting, and how this leads to ambiguous redirection errors?Thanks in advance!
https://redd.it/1lf6ccw
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Terminal Commands That I Use to Boost Programming Speed
https://medium.com/gitconnected/terminal-commands-that-i-use-to-boost-programming-speed-e76b6ef07cb0?sk=84dc6150ea662198080fe12a1f4a0b81
https://redd.it/1lfx2m3
@r_bash
https://medium.com/gitconnected/terminal-commands-that-i-use-to-boost-programming-speed-e76b6ef07cb0?sk=84dc6150ea662198080fe12a1f4a0b81
https://redd.it/1lfx2m3
@r_bash
Medium
Terminal Commands That I Use to Boost Programming Speed
Double your programming productivity with these Linux/Unix/Bash commands
smart-pause-resume: An automation noscript designed for managing multiple media players on Linux efficiently.
https://github.com/erenseymen/smart-pause-resume
https://redd.it/1lg5dbw
@r_bash
https://github.com/erenseymen/smart-pause-resume
https://redd.it/1lg5dbw
@r_bash
GitHub
GitHub - erenseymen/smart-pause-resume
Contribute to erenseymen/smart-pause-resume development by creating an account on GitHub.
how to make my bash code stop if the package manager is not valid?
https://preview.redd.it/nplcu96sng8f1.png?width=1920&format=png&auto=webp&s=00339818a850fa1992587e82186ca7269a4bf324
https://redd.it/1lhlbc2
@r_bash
https://preview.redd.it/nplcu96sng8f1.png?width=1920&format=png&auto=webp&s=00339818a850fa1992587e82186ca7269a4bf324
https://redd.it/1lhlbc2
@r_bash
Getting parent dir of file without path in one step in pure bash?
Is there an easy way to get the parent dir of a file without the path in pure bash? Or, in other words, get the substring of a variable between the last and next-to-last slash?
I know of
path='/path/to/pardir/file'
dirpath="${path%/}"
pardir="${dirpath##/}"
echo "$pardir"
pardir
With awk:
$ awk -F '/' '{sub(/\.^.+$/, "", $NF); print $(NF-1)}' <<< "$s"
$ pardir
and there's also
Is there an easy, one-step incantation with pure bash so I can get this substring between the two last slashes?
https://redd.it/1li8ylt
@r_bash
Is there an easy way to get the parent dir of a file without the path in pure bash? Or, in other words, get the substring of a variable between the last and next-to-last slash?
I know of
path='/path/to/pardir/file'
dirpath="${path%/}"
pardir="${dirpath##/}"
echo "$pardir"
pardir
With awk:
$ awk -F '/' '{sub(/\.^.+$/, "", $NF); print $(NF-1)}' <<< "$s"
$ pardir
and there's also
expr match, although I'm not good with regexes. Not to mention dirname and basename.Is there an easy, one-step incantation with pure bash so I can get this substring between the two last slashes?
https://redd.it/1li8ylt
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community