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
Update to Bash Strict Mode README
My README guettli/bash-strict-mode: Bash Strict Mode got updated.
Feedback is welcome: Please tell me, if you think something could get improved.
https://redd.it/1ljxt7e
@r_bash
My README guettli/bash-strict-mode: Bash Strict Mode got updated.
Feedback is welcome: Please tell me, if you think something could get improved.
https://redd.it/1ljxt7e
@r_bash
GitHub
GitHub - guettli/bash-strict-mode: Bash Strict Mode
Bash Strict Mode. Contribute to guettli/bash-strict-mode development by creating an account on GitHub.
bash2json - fully bash-written JSON parser
so, firstly it was created as a simple parser function for my another project, but i kinda wanted to make full JSON support in vanilla bash including arrays support, so it's fully written using bash substitution and builtins
i'd be happy to listen to any critics/suggestions
https://github.com/Tirito6626/bash2json
https://redd.it/1lkhk4i
@r_bash
so, firstly it was created as a simple parser function for my another project, but i kinda wanted to make full JSON support in vanilla bash including arrays support, so it's fully written using bash substitution and builtins
i'd be happy to listen to any critics/suggestions
https://github.com/Tirito6626/bash2json
https://redd.it/1lkhk4i
@r_bash
GitHub
GitHub - Tirito6626/bash2json: The most bash-based JSON parser
The most bash-based JSON parser. Contribute to Tirito6626/bash2json development by creating an account on GitHub.