r_bash – Telegram
In What File $LSColors is Defined?

Hi all

Can you please tell me in what file the $LS
Colors variable is defined?

Thank you

https://redd.it/1lt2yh5
@r_bash
BASH LEARN

Hi everyone! 👋 I’m new to this subreddit and currently learning Bash through a really good guided course.

I’d love to know how I can complement what I’m learning. As you all know, in the IT world, curiosity is key — and it’s always good to go beyond the course.

Any resources, challenges, projects, or practice ideas you’d recommend to get better at Bash? Thanks in advance!


https://redd.it/1lv0bld
@r_bash
we're finally getting output capture without forkinf in bash 5.3
https://redd.it/1lvclso
@r_bash
"Bash 5.3 Release Adds 'Significant' New Features

🔧 Bash 5.3 introduces a powerful new command substitution feature — without forking!

Now you can run commands inline and capture results directly in the current shell context:

${ command; } # Captures stdout, no fork
${| command; } # Runs in current shell, result in $REPLY

Faster
State-preserving
Ideal for noscripting

Try it in your next shell noscript!




https://redd.it/1lwzhpm
@r_bash
All about ${| cmd; REPLY=value;} in bash 5.3

Sadly, ChatGPT has been spreading misinformation this week. Tech journalist should really do a better job when prompting it to write articles for them.

>${| command; } runs in the current shell and leaves result in REPLY

The first part is accurate, the new command substitution construct does indeed run in the same execution environment just like its sibling ${ command;}, the 2nd part about REPLY is wrong.

Everybody know and loves when read gets a herestring without a variable name being mentioned, and then bash politely assigns by default the value to REPLY.

$ read <<<foo
$ declare -p REPLY
declare -- REPLY="foo"

In the new construct, things don't work like this. The whole point is to get your hands dirty and manually assign a value to REPLY inside ${...;}.

Long story short, if the bash interprets sees a line like this.

main-command ${|cmd;REPLY=value;} arg2

Firstly, the interpreter will evaluate the cmd inside command substitution and dump the result on stdout or stderr, depending on the case.
The value assigned to REPLY is going to become the argument to the main command. In this case, arg1.

Example:

$ printf '%s\n' foo ${|uname; REPLY=bar;} baz
Linux
foo
bar
baz

The main-command is printf and cmd in this case is uname which on my system returns Linux (if you are on macOS you get Darwin).
The first thing that gets printed on stdout is Linux (the result of uname) even though foo is the first argument for printf. Next foo gets printed, then bar gets inserted inline as arg2 for printf because it is the value assigned to REPLY inside ${...;}.

Now you don't have to limit yourself REPLY=something syntax.

$ printf '%s\n' foo bar ${|uname; read <<<baz;}
Linux
foo
bar
baz

You can set REPLY inside ${...;} without even typing its name. Hell, if you want everybody to hate your guts, you can even do something like this:


$ update () {
local -n ref=$1
ref=foo
}
$ printf '%s\n' ${| update REPLY; uname;} bar baz
Linux
foo
bar
baz

It does not matter if you first assign the value to REPLY and then write the cmd inside ${...;}, in fact you can skip either cmd or REPLY. Skipping cmd:

$ echo "hi, ${|REPLY=five;}"
hi, five

Skipping REPLY:

$ printf "${|pwd;}"
/tmp/news

If REPLY assignment isn't valid, that is: no stdin, than REPLY is empty, and you get a message printed on stderr:

$ echo "This is a: ${|REPLY=$((4/0));} value."
bash: 4/0: division by 0 (error token is "0")
This is a: value.

Finally, REPLY inside the new command substitution variant is pretty much local.

>Bash creates REPLY as an initially-unset local variable when command executes, and restores REPLY to the value it had before the command substitution after command completes, as with any local variable.

So:

$ REPLY=foo
$ declare -p REPLY
declare -- REPLY="foo"
$ echo "This is ${|REPLY=bar;}"
This is bar
$ declare -p REPLY
declare -- REPLY="foo"


P.S. Don't forget to quote the new variant of command substitution if you want to avoid word splitting and filename expansion, just like with the old variant.

https://redd.it/1lxj1c7
@r_bash
First post here . . . maybe stupid but it works.

between bash, mpv and fzf . . .and a python noscript i wrote, I have my favorite music setup on a computer ever.

fzf really is a golden terminal tool . . . so many options.

#!/bom/bash

set -e

set -o pipefail

trap 'echo "Exiting..."; exit 1' SIGINT

album_file=$(find -L ~/Music -type d | fzf )

if [ -f "$album_file/playlist.m3u" ]; then

echo "playlist.m3u exists"

else

cd "$album_file" && mk_album

echo "created playlist.m3u"

fi

mpv "$album_file/playlist.m3u"

mk_album is my python noscript that creates the mpv playlist if it isn't there . . .by track number, not just alphabetical contents of what is in the folder.

Happy Saturday

https://redd.it/1ly6nf3
@r_bash
what is the best bash version to keep noscripts in?

now i myself prefer the last bash version, which is supported in my environment (bash 5.2.15 as for Debian 12), but i'm also testing 5.3 and it got me thinking, which version is the best to write noscripts in

the latest one for perfomance and features
OR
the oldest popular one for support (e.g. 3.4)


https://redd.it/1lyeyv8
@r_bash
When in vi mode, how to start off in command mode?

Okay, so I've been using Bash's vi mode for two decades. One thing I should have inquired about from the start, is this: is it possible to make command mode default, instead of insert mode? I notice that for nearly every command the first thing I do is hit <ESC> to go into command mode, so that I can go up and down the command-history list using <k> and <j>. It'd be great if command mode could somehow be made the default.

Thanks all.

https://redd.it/1lxw2qo
@r_bash
noscript kind of works

For a class I'm taking I need to write a bash noscript that takes two arguments a number and a word. The noscript should create a number of files equal to the number entered and the file should be named (word argument)_(number). the number needs to be at least 2 digits so 9 should be 09.

I've written a noscript that works most of the time, except when the number is 8 or 9. it works for 7 and down, and also from all numbers above 9 until it reaches 9. It also doesn't change if the input is 09 or 9


Here the noscript:

>\#!/bin/bash
\#create variables
NUM=$1
NAME=$2
\#print variables
echo $NUM
echo $NAME
\#make directory to hold files
mkdir $NAME"_Files"
\#make files and place them in directory
while [ $NUM -ge 1 \]; do
if [ "${#NUM}" -le 1 \];then
NUM="0"$NUM
fi
touch $NAME"_Files"/$NAME"_"$NUM
NUM=$((NUM-1))
done

Any help would be appreciated

https://redd.it/1lyv4xy
@r_bash
Using both subcommands and getopts short options? Tips

I have a wrapper noscript where I first used short options with getopts because my priority is typing as little as possible on the CLI. Then I realized some options need more than one required argument, so I need to use subcommands.

How to use both? It probably makes sense to use ./noscript [sub-command] with different short options associated with specific subcommands, so I need to implement getopts for each sub-command or is there an easier or less involved way? I'm thinking I need to refactor the whole noscript to try to reduce as much short options that are only specific to a subcommand as much as possible so that for argument parsing, I first loop through the arguments stopping when it sees one that starts with a - where processed ones are treated as subcommands, then process the rest with getopts. Then for subcommands that take unique short options, use getopts for that subcommands?

Any suggestions are much appreciated, I don't want to make this a maintenance nightmare so want to make sure I get it right.

https://redd.it/1m01rv2
@r_bash
One-encryption

Hi, I was learning some bash noscripting, but then I had a doubt, like, I know how to encrypt and decrypt with openssl:

# Encrypt
echo "secret" | openssl enc -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD
# Decrypt
echo "<HASH> | openssl enc -d -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD

But that's not what I want now, I'm looking for a one-way encryption method, a way that only encrypts the data and the result is to verify if the user input matches the encrypted information(probably using a if statement for the verification). Example:

#!/usr/bin/env bash

ORIGINAL=$(echo "sponge-bob" | one-way-encrypt-command)

read -rp "What is the secret?" ANSWER
if "$(echo $ANSWER | one-way-encrypt-command)" = "$ORIGINAL" ; then
echo "Yes you're right!"
else
echo "Wrong!"
fi

https://redd.it/1m01yan
@r_bash
Bash 5.3 - first 'huh?' moment.

Hello.

Trying out some of the new features in bash 5.3, and have come across my first 'huh?' moment.

% export TEST=aaabbb
%
% echo $( sed 's/a/b/g' <<< $TEST ; )
bbbbbb

% echo ${ sed 's/a/b/g' <<< $TEST ; }
sed: couldn't flush stdout: Device not configured

% echo ${| sed 's/a/b/g' <<< $TEST ; }
bbbbbb

Can anyone explain why the 2nd version doesn't work?

Thanks

fb.

https://redd.it/1m0i8um
@r_bash
you guys could really like this simple function!!!!

maybe i'm a really really specific kind of user but sometimes i genuinely forget whether i wanna go to a directory or a file

if you use bash completions as a file manager, you could also replace $EDITOR with $PAGER

c() {
if -f "$1" ; then
"${EDITOR:-vi}" "$1"
else
cd "${1:-$HOME}"
fi
}

https://redd.it/1m0tn71
@r_bash
Handling bash settings across distros

Recently I have started keeping track of my dotfiles as I work with more and more machines, I thought it appropriate to start tracking them and syncing them across my machines. Simple enough.

However, bash is proving to be specially hard to do this with. Most of my dotfiles are programs I install and configure from scratch (or at least parting from virtually identical defaults), however, with bash, I have to worry about profiles, system configs differing across distros, etc...

Basically, I have 3 machines, one is on Fedora, another is on Tumbleweed and another is on Debian. Each of these is doing COMPLETELY different things in /etc/bash.bashrc or /etc/bashrc and the default .bashrc is also doing completely different things. And that is without even considering profile files and other files like .bash_logout and such.

How can I sync my .bashrc files without having to manually manage system files in each system (and any potential future system). Or simply, how have you solved this issue for your own setup? Do I just sync whatever I create and disregard system configs? Any advice?

https://redd.it/1m1dfpx
@r_bash
I dare you to hack my secure login system ;)

Note: Run with $ ./login.sh, not $ sh `login.sh`, or else it will not run with bash and will break. But you probably already knew that.

#!/bin/bash
clear
echo "CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)"
echo "########################"
echo "# #"
echo "# Login Terminal #"
echo "# #"
echo "########################"
echo ""
select item in "Login" "Register"; do
case $REPLY in
1)
clear
echo "LOGIN"
echo "#####"
echo ""
echo -n "Username: "
read uname
if test -d "$uname"; then
cd $uname
echo -n "Password: "
read -s pwd
pwdsm=$(< pwd)
pwdsu=$(echo $pwd | sha256sum)
if $(echo $pwdsm | cut -f 1 -d " ") = $(echo $pwdsu | cut -f 1 -d " ") ; then
clear
echo Login Succesful!
break
else
echo Error: Incorrect password.
break
fi
else
echo Error: Incorrect Username.
break
fi
;;
2)
clear
echo "REGESTER"
echo "########"
echo ""
echo -n "Username: "
read uname
mkdir $uname
cd $uname
echo -n "Password: "
read -s pwd
echo $pwd | sha256sum > pwd
echo ""
break
;;
esac
done

https://redd.it/1m2eumm
@r_bash
imagemagick use image from clipboard

#!/bin/bash

DIR="$HOME/Pictures/Screenshots"
FILE="Screenshot_$(date +'%Y%m%d-%H%M%S').png"

gnome-screenshot -w -f "$DIR/$FILE" &&
magick "$DIR/$FILE" -fuzz 50% -trim +repage "$DIR/$FILE" &&
xclip -selection clipboard -t image/png -i "$DIR/$FILE"
notify-send "Screenshot saved as $FILE."


This currently creates a file, then modifies it, saves it as the same name (replacing)

I was wondering if it's possible to make magick use clipboard image instead of file. That way I can use --clipboard with gnome-screenshot. So I don't have to write file twice.

Can it be done? (I am sorry if I am not supposed to post this here)

https://redd.it/1m2v043
@r_bash
In hunt of productivity tools in bash (to list in devreal.org)

Modern software development feels like chasing smoke, frameworks rise and fall, GUIs shift faster than we can learn them, and the tools we depend on are often opaque, bloated, or short-lived.

I think the terminal is where real development will happen. The long-lasting inventions on how to work with the computer will be made in the terminal. Now even more, with AI, it is easier for an agent to execute a command than to click buttons to do a task.

*I am creating a list productivity applications* in "devreal.org". Do you know of any applications that meet the criteria? Do you have any good idea to add to the project?

* [https://devreal.org](https://devreal.org)

https://redd.it/1m2y6br
@r_bash