What is the best use case of sed? How should I learn it?
For atleast a day, I had been reading man pages regarding the sed command. I felt the syntax pretty confusing. Is there any way to keep in mind the pattern and regex?
Tell me how you learnt the sed command the first time.
https://redd.it/1rh4vox
@r_bash
After we had the braces, don't forget to give the brackets some love.
I was surprised that the brace tip was news to so many Redditors.
Just as a reminder, you can also use brackets in a lot of situations:
$ ls /dev/sda-d?
/dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdc2 /dev/sdc3 /dev/sdc4 /dev/sdc5 /dev/sdc6 /dev/sdc7 /dev/sdc8 /dev/sdc9 /dev/sdd1 /dev/sdd2
If you embrace the braces, you can also make a racket about brackets!
https://redd.it/1rbh7yp
@r_bash
cd - is the fastest way to bounce between two directories
Instead of retyping:
cd /var/log/nginx
Just type:
cd -
It teleports you back to wherever you just were. Run it again and you're back. It's Alt+Tab for your terminal.
Real world use case — you're tailing logs in one directory and editing configs in another:
cd /var/log/nginx
tail -f access.log
cd /etc/nginx/conf.d # edit a config
cd - # back to logs instantly
cd - # back to config
Bonus: $OLDPWD holds the previous directory if you ever need it in a script:
cp nginx.conf $OLDPWD/nginx.conf.bak
Works in bash and zsh. One of those things you wonder how you lived without.
https://redd.it/1rb2d32
@r_bash
Why can't I ever access to https://www.gnu.org/ :403 Forbidden
**Hi**, Why can't I ever access this website https://www.gnu.org/ ?
for example this: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
Forbidden
You don't have permission to access this resource.
https://redd.it/1r90wfg
@r_bash
Terminal Phone - E2EE PTT Walkie Talkie
https://redd.it/1r91uzp
@r_bash
Issues with ble.sh
I wanted to try autocomplete and suggestions based on history in bash and installed ble.sh
It is giving me initialisation issues with rendering my starship prompt
this is my bashrc
# ble.sh auto completion
[ $- == *i* ] && source /usr/share/blesh/ble.sh
eval "$(starship init bash --print-full-init)"
bind "set completion-ignore-case on"
alias ls='eza -lh --icons --color=auto --group-directories-first'
alias ll='eza --icons --group-directories-first'
alias la='eza -a --icons --group-directories-first'
alias lla='eza -lah --icons --group-directories-first'
alias tree='eza --tree --icons'
alias grep='grep --color=auto'
alias cls='clear'
alias rb='source ~/.bashrc'
#PS1='\u@\h \W\$ '
when i am opening a new terminal instead of defaulting a starship prompt it gives me something like this
catppuccinno@catppuccinnoLPTP ~$
when i do a clear command then the default starship prompt comes back, like this
~
❯
can anyone help with this ?
https://redd.it/1raj21f
@r_bash
I rewrote GNU Stow in pure Bash
A while back I installed GNU Stow via pacman to manage my dotfiles. It pulled in Perl and a bunch of deps, ran it, and got a syntax error (i don't remember which). Had to sudo vim /usr/bin/stow to add parentheses somewhere around line 500 to make it stop erroring out. No idea why Perl was choking on it; I just chose to use Bash, and then later on, made this.
So I wrote `bstow`, a drop-in replacement for GNU Stow (), in pure Bash. No dependencies, just a single script you can throw directly into your repo and it works anywhere Bash does.
> () regex flavor on Bash depends on the platform
It's actually faster than Stow in some cases and has a few things Stow doesn't, like dynamic ignore rules via a script on top of the .stow-ignore.
I use a single repo across both Termux and regular Linux for my bash scripts; my filter script looks like this:
if -v TERMUX_APP__PACKAGE_NAME ; then
# head -n-1, since this file is first result here
grep -lr 'include barg.sh' | sed 's#./##' | head -n-1
printf '%s\n' lpwa web-kiosk-wrap
else
grep -lr '^# termux only$' | sed 's#./##'
fi
Termux gets its packages, Linux gets its packages, same repo, no manual management.
Has dotfile transformation (dot-bashrc → .bashrc), simulation mode (-n), bash regex ignore patterns (bionic regex in Termux, it depends on the libc implementation), and force mode (overwrite). Drop the script in, chmod +x, done; git keeps the file permissions.
https://redd.it/1ra7zdh
@r_bash
How I made my .bashrc modular with .bashrc.d/
This might be obvious to a lot of you, sourcing a directory instead of one massive file is a pretty common pattern. But i still see plenty of 500-line .bashrc files in the wild, so maybe not everyone's seen it.
My .bashrc was 400+ lines. Everything dumped in one place.
I made it modular. Source a directory instead of one file:
if [ -d "$HOME/.bashrc.d" ]; then
for config in "$HOME/.bashrc.d"/*.sh; do
[ -r "$config" ] && source "$config"
done
fi
~/.bashrc.d/
├── 10-clipboard.sh
├── 20-fzf.sh
├── 22-exa.sh
├── 25-nvim.sh
├── 30-project-workflow.sh
└── 40-nvm.sh
How to recursively extract zip files in a directory hierarchy
I've downloaded a massive archive of files with a file hierarchy something like this
filesFrom2008
├type1
│├A
││├AA.zip
││├AB.zip
││etc.
││
│├B
││├BA.zip
││├BB.zip
││etc.
││
│etc.
│
├type2
etc.
filesFrom2009
├type1
etc.
I need some help figuring out how I can extract all of these. I don't care about preserving the file hierarchy as I'm going to re-sort the files my own way, I just need the thousands of files extracted, ideally without taking forever since the whole archive is over 60gb of mostly teeny tiny files.
And yes this is a terrible way to package files, it's not my fault; I didn't do this.
https://redd.it/1r56hvt
@r_bash
bash pecularities over ssh
I have a machine where I login over ssh, or just use `ssh server command` as a shortcut.
Now there are some unexpected behaviors, and I can't make head or tail of what happens. Maybe the /r/bash community can help, and how to avoid it?
Here is what happens:
spry@E6540:~$ ssh nuc10i3fnk.lan ls -1tdr "/srv/media/completed/**/*ODDish*"
ls: cannot access '/srv/media/completed/**/*ODDish*': No such file or directory
spry@E6540:~$ ssh nuc10i3fnk.lan ls -1tdr /srv/media/completed/**/*ODDish*
ls: cannot access '/srv/media/completed/**/*ODDish*': No such file or directory
spry@E6540:~$ ssh nuc10i3fnk.lan 'ls -1tdr /srv/media/completed/**/*ODDish*'
ls: cannot access '/srv/media/completed/**/*ODDish*': No such file or directory
spry@E6540:~$ ssh nuc10i3fnk.lan
spry@nuc10i3fnk:~$ ls -1tdr /srv/media/completed/**/*ODDish*
# <the expected results are found>
spry@nuc10i3fnk:~$
To sum it up: I have `shopt -s globstar` in my `~/.bashrc`.
When I try to list some files with a `**` in the command, it works when I am on the server, but not when I issue the `ls` command via `ssh server command`.
I tried some combinations of quotes around path and command, but it didn't help. Is there a way to fix this so I can use server command` instead of logging in?
https://redd.it/1r3u15t
@r_bash
Help with a script that used to work but won't anymore?
The problem function is here (Github repo with the full script)
I can't tell what's broken, I've spent some time jiggling parts around](https://imgur.com/a/dpPpUF8) to... at least break it a different way. JQ isn't filtering the jsons correctly when in the script and always returns null, even though it works just fine when I run the same thing in the terminal, even when the filtering input is a variable like it is in the script. Single, regular, or no quote aren't affecting the situation.
https://redd.it/1r4b27g
@r_bash
Wrapper Script Accessing Root-owned Variables
I've got a systemd timer that automatically backs up important files remotely using restic. It uses a root-owned (700 permissions) environment file for the secret keys and repository password. Systemd works as expected. Occasionally, I want to verify snapshots or manage backups manually, but I want to use the **same environment file**. So I wrote a wrapper script for restic to do this.
I was having trouble using `source` to load the environment variables with `sudo`. I understand that's because `source` is a bash built-in, so it wouldn't work. But I didn't want to define 4 variables manually each time, either. I ended up using a here-document. It works fine, but I'm wondering how to improve it or keep myself out of trouble.
#!/bin/bash
sudo bash<<EOF
set -a
. /etc/restic/restic-backblaze.env
set +a
restic "$@"
EOF
After testing my script, I found this here as well: https://www.reddit.com/r/bash/comments/qubjar/what_is_the_best_way_to_run_a_specific_function/hkpspt6/. That's kind of validating, but I want to confirm.
1. Do I need to have `set +a` since this is running in a subshell?
1. Will my secrets and password be unset automatically once the script completes? I didn't see them in my user `env` list but are they elsewhere?
1. Should I change the first `EOF` to `'EOF'` with the quotes?
1. Is it really this straightforward?
Thanks in advance.
https://redd.it/1r4up5a
@r_bash
BASHAM! : A Simple Bash Script to Manage Your Assembly Projects.
I've been fooling away my days by doing my hobbies. I was supposed to learn assembly but my idiotic ass learnt bash scripting instead. At least I can still learn assembly a bit with it...
BASHAM!
Yeah, that's the repo. I'm looking for attention so I get some more contributors... 🫤
https://redd.it/1jynn7o
@r_bash
Replacing echo with printf broke my scripts
Taking the advice in https://www.reddit.com/r/bash/comments/1519wby/why\_printf\_over\_echo\_noob\_question/ and elsewhere, I proceeded to dosed -i 's/echo /printf \x27%s\\n\x27 /' bin/*.sh
Whereas echo had worked perfectly, many strings now mysteriously got truncated. I reverted back to echo and all is working well, again, but I'm intrigued why this happened. I tried replacing %s with %b but it made no difference.
Does printf %s not handle utf-8 correctly or something?
https://redd.it/1jxi95b
@r_bash
Actual Vim Motions in bash?
I only found like two posts on reddit and another on StackOverFlow where the user is expressing frustration of set -o vi and seeking an alternative approach to get vim motions in his shell, which is very shocking to me but maybe I just suck at googling right? or people know their shell keybinings unlike me - lazy to learn something new. Anyway, I found this project which pulls it off: https://github.com/akinomyoga/ble.sh
The problem though is that there's noticeable latency that I can't wrap my head around, so if someone uses this and it doesn't have latency please tell me what terminal emulator you use.
https://redd.it/1jwrevg
@r_bash
xytz - a beautiful TUI YouTube Downloader app
https://www.reddit.com/gallery/1r157je
https://redd.it/1rgw10z
@r_bash
The Bash Reference Manual shows up in Epstein files.
https://redd.it/1rb1wd0
@r_bash
Stop typing the filename twice. Brace expansion handles it.
Stop typing the filename twice. Brace expansion handles it. Works on any file, any extension.
\#Instead of
cp config.yml config.yml.bak
\#Do
cp nginx.conf{,.bak}
cp .env{,.bak}
cp Makefile{,.$(date +%F)}
\# That last one timestamps your backup automatically. You're welcome.
https://redd.it/1rax3ds
@r_bash
Check Epstein Files into Version Control With GitEpstein
I made two simple bash scripts, one that loops through the epstein files to download each file and then another bash script that runs that other one and commits the changes into git so you have timestamped changes of specific files.
https://github.com/Goldie323/GitEpstein
https://redd.it/1r95kz3
@r_bash
Is 'eval' tool ok to use with variables or is there another alternative I could use?
I'm using a number of chained commands in bash to query the current state of various system settings. To help keep the chain from becoming excessively long, I've created a number of variables, many of which are also used in other areas of this project.
The issue I've come to realize are these variables set a static value based on the state of the system setting at the time they were created. For most of these variables, this is exactly what I need them to do. But there are some where I need the variable to provide a dynamic value based on the current state of a setting.
For example, say I wanted a report to include the current timestamp, the variables I have been using are similar to this:
user@ubuntu:~$ date=$(echo -en "Report Date:\t"; date | cut -c 5-28);
user@ubuntu:~$ echo "$date"
Report Date: Feb 20 06:14:28 UTC 2026
user@ubuntu:~$ echo "$date"
Report Date: Feb 20 06:14:28 UTC 2026
This does not entirely work as needed since the variable simply provides same value as it was when created. After some online searches and reading, a solution I found was to quote the command when creating the variable and then use the 'eval' tool to act on the variable. For example:
user@ubuntu:~$ date="echo -en \"Report Date:\t\"; date | cut -c 5-28"
user@ubuntu:~$ eval "$date"
Report Date: Feb 20 06:15:07 UTC 2026
user@ubuntu:~$ eval "$date"
Report Date: Feb 20 06:16:12 UTC 2026
This seems to resolve my issue. However, throughout the online readings, the general consensus seems to be that 'eval' should be avoided as it can unintentionally or nefariously be used to arbitrarily enable code executions.
Based on the above example, would the use of 'eval' be ok/safe in this case or is there perhaps an alternative option that could achieve the same results?
https://redd.it/1r9pnn4
@r_bash
Stop letting your shell hold you back. I created a ZSH config that has ~20ms lag. with all the modern features.
https://redd.it/1rak4qh
@r_bash
How to optimize the cd command to go back multiple folders at once
https://redd.it/1ra7o2o
@r_bash
trick to make your usage look nice
https://redd.it/1r67c5e
@r_bash
Strange dirname/pwd processing
EDIT: Solved by u/kolorcuk
EDIT 2: For people seeing this in the future - put 'unset CDPATH' in your script.
This is really strange. I'm looking at some code that fails on a build and the following sniplet is puzzling. It almost appears that the && is concatenating the dir.
The following fails for my bash but other devs bash's work:
\------
setup - create /tmp/bob/tmp.
Add the following script as /tmp/bob/tmp/tst.sh:
\----- snip ------------------------
#!/bin/bash
set -e
set -x
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
echo "SCRIPT_DIR: " $SCRIPT_DIR
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
echo "Project root: $PROJECT_ROOT"
echo ""
cd "$PROJECT_ROOT"
\---- snip ----------------------
Running the script from /tmp/bob as $ bash tmp/tst.sh
Returns an error
tmp/tst.sh: line 14: cd: $'/tmp/bob/tmp\\n/tmp/bob': No such file or directory
Note the \\n in the string. The odd thing is that it works if you go into tmp or if you call it from /home/<my user>. AND even stranger is that it works on other peoples bash.
When it's broken we get: ( note: <new line> added by me for clarity)
$ bash tmp/tst.sh
+++ dirname tmp/tst.sh
++ cd tmp
++ pwd
+ SCRIPT_DIR='/tmp/bob/tmp <new line>
/tmp/bob/tmp'
+ echo 'SCRIPT_DIR: ' /tmp/bob/tmp /tmp/bob/tmp
SCRIPT_DIR: /tmp/bob/tmp /tmp/bob/tmp
++ dirname '/tmp/bob/tmp <new line>
/tmp/bob/tmp'
+ PROJECT_ROOT='/tmp/bob/tmp <new line>
/tmp/bob'
+ echo 'Project root: /tmp/bob/tmp <new line>
/tmp/bob'
Project root: /tmp/bob/tmp <new line>
/tmp/bob
+ echo ''
+ cd '/tmp/bob/tmp
/tmp/bob'
tmp/tst.sh: line 14: cd: $'/tmp/bob/tmp\n/tmp/bob': No such file or directory
https://redd.it/1r284np
@r_bash
Aesthetic and minimalist typing test with somewhat accurate feedback
https://zen-type-brown.vercel.app
I tried to make it as Visually pleasing as possible and to make each test meaningful.
Before actually deploying it I wanted to know any bugs or new features to be added and I would appreciate any reviews on this project.
https://redd.it/1r412am
@r_bash
Simple coding tool in bash.
Hi. I've made a simple encoder and decoder tool. You can prank your friends sending a message in base64 or ROT-13. here is the tool: https://github.com/iangrinan0-cmd/decoder-lab
https://redd.it/1r4qeub
@r_bash
How to execute a program in a new terminal window?
Quick question:
How do I execute a program in a new terminal window?
I wrote a Go CLI program ("CLauncher"), that I'd like to run when I hit the Win+R shortcut.
I setup the shortcut to run a `runner.sh` script, which should open a terminal executing CLauncher with the runw argument. (CLauncher runw)
Do I call bash with a specific option. Like bash run Clauncher runw?
Or is there a specific shell command to use?
I'm using Konsole (Kubunu), so I tried: konsole -e CLauncher runw
And that works almost as expected. Opens a new terminal with the program running. But once I try calling it from a shell script, nothing happens. It works when I open the terminal first and then run the shell script.
Edit:
~~/bin/bash -c CLauncher runw~~ /bin/bash -c "CLauncher runw" starts the program as well, but no window. It's basically hidden. What option am I missing?
BTW: The CLauncher program does not terminate. It waits for user input. So, I don't think it's the case of bash just executing, being done and closing quickly.
https://redd.it/1r4r5xb
@r_bash
Why does this work?
arg2list() {
local toset=$1
shift 1
# shellcheck disable=SC2145
# we actually want to eval on structured data.
# so mixing strings with arrays is the point
# shellcheck disable=SC2294
# and yes eval on a string negates the benefits of arrays,
# thats why we leave it an array.
eval "$toset=($@)"
}
addFlags() {
local n flag before after var
# Disable file globbing, since bash will otherwise try to find
# filenames matching the the value to be prefixed/suffixed if
# it contains characters considered wildcards, such as `?` and
# `*`. We want the value as is, except we also want to split
# it on on the separator; hence we can't quote it.
local reenableGlob=0
if [[ ! -o noglob ]]; then
reenableGlob=1
fi
set -o noglob
# shellcheck disable=SC2086
arg2list before $1
# shellcheck disable=SC2086
arg2list after $2
if (( reenableGlob )); then
set +o noglob
fi
var="argv_tmp"
printf '%s\n' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);"
printf '%s\n' "${var}[0] = argv[0];"
for ((n = 0; n < ${#before[@]}; n += 1)); do
flag=$(escapeStringLiteral "${before[n]}")
printf '%s\n' "${var}[$((n + 1))] = \"$flag\";"
done
printf '%s\n' "for (int i = 1; i < argc; ++i) {"
printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
printf '%s\n' "}"
for ((n = 0; n < ${#after[@]}; n += 1)); do
flag=$(escapeStringLiteral "${after[n]}")
printf '%s\n' "${var}[${#before[@]} + argc + $n] = \"$flag\";"
done
printf '%s\n' "${var}[${#before[@]} + argc + ${#after[@]}] = NULL;"
printf '%s\n' "argv = $var;"
}
I need to know why this works.
Why does this function preserve the arg escaping correctly? I sorta get it, and I sorta don't. Is there a better way to do this that works in posix sh like this does?
All the explanations written in the PR are by me, they represent my current understanding, as are the explanations underneath the shellcheck disables.
Is my understanding correct?
arg2list() {
local toset=$1
shift 1
# shellcheck disable=SC2145
# we actually want to eval on structured data.
# so mixing strings with arrays is the point
# shellcheck disable=SC2294
# and yes eval on a string negates the benefits of arrays,
# thats why we leave it an array.
eval "$toset=($@)"
}
addFlags() {
local n flag before after var
# Disable file globbing, since bash will otherwise try to find
# filenames matching the the value to be prefixed/suffixed if
# it contains characters considered wildcards, such as `?` and
# `*`. We want the value as is, except we also want to split
# it on on the separator; hence we can't quote it.
local reenableGlob=0
if [[ ! -o noglob ]]; then
reenableGlob=1
fi
set -o noglob
# shellcheck disable=SC2086
arg2list before $1
# shellcheck disable=SC2086
arg2list after $2
if (( reenableGlob )); then
set +o noglob
fi
var="argv_tmp"
printf '%s\n' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);"
printf '%s\n' "${var}[0] = argv[0];"
for ((n = 0; n < ${#before[@]}; n += 1)); do
flag=$(escapeStringLiteral "${before[n]}")
printf '%s\n' "${var}[$((n + 1))] = \"$flag\";"
done
printf '%s\n' "for (int i = 1; i < argc; ++i) {"
printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
printf '%s\n' "}"
for ((n = 0; n < ${#after[@]}; n += 1)); do
flag=$(escapeStringLiteral "${after[n]}")
printf '%s\n' "${var}[${#before[@]} + argc + $n] = \"$flag\";"
done
printf '%s\n' "${var}[${#before[@]} + argc + ${#after[@]}] = NULL;"
printf '%s\n' "argv = $var;"
}
A universal CLI run command for projects?
https://github.com/pran13-git/Vroom
https://redd.it/1jw7rjp
@r_bash