r_bash – Telegram
Is this a minor shellcheck omission?

Assigning a variable that starts with a digit inside an arithmetic context.

#!/bin/bash
1num=$((10+1))
((num=1+2))
((2num=10+2))
echo $num "$2num"


Shellcheck 0.10.0 bash 5.1.16. 2num on line 4 isn't marked as an error by shellcheck.

$ shellcheck --color=never test.sh

In test.sh line 2:
1num=$((10+1))
^------------^ SC2282 (error): Variable names can't start with numbers, so this is interpreted as a command.

For more information:
https://www.shellcheck.net/wiki/SC2282 -- Variable names can't start with n...
$ bash -x test.sh
test.sh 2 1num=11
test.sh: line 2: 1num=11: command not found
test.sh 3 (( num=1+2 ))
test.sh 4 (( 2num=10+2 ))
test.sh: line 4: ((: 2num: value too great for base (error token is "2num")
test.sh 5 echo 3 num
3 num


What does value too great for base error actually mean?

https://redd.it/1bt54vz
@r_bash
Fetching value from .json file

I've got a .json file that contains the following:

"shared":
{
"delimiter": "|",
"queueFileName": "~/Documents/SendEmailQueue.txt"
}

Then I've got a noscript that fetches the queueFileName value via:

queueFileName=$(jq -r '.shared.queueFileName' Backup.json)

In an attempt to figure out what's happening, specifically the noscript is not finding the file in question, I added the following bit of code:

if -f "${queueFileName}"
then
echo "Queue file, \"${queueFileName}\", is present"
if -s "${queueFileName}"
then
echo "... and it's not empty!"
else
echo "... but empty!"
fi
else
echo "Queue file, \"${queueFileName}\", not found"
fi

When I run the noscript, the following is displayed:

Queue file, "~/Documents/SendEmailQueue.txt", not found

However... if I change the "queueFileName" value in the .json file, (removing the "\~/" shortcut), to this;

"/home/babbayagga/Documents/SendEmailQueue.txt"

The following is displayed in the terminal window:

Queue file, "/home/babbayagga/Documents/SendEmailQueue.txt", is present
... and it's not empty!

It's as if the "\~/" shortcut is not being expanded. Does anyone know why this might be happening?

Thanks!

https://redd.it/1btav5e
@r_bash
What's the best way to find supported colors programmatically?

** Note: GoLang is referenced, but the post is mostly about the bash environment

What's the best way to find all supported colors of a bash shell programmatically?

I'm making an app that helps with formatting text with colors, made with Go, but built for Linux shells. It's working, but I'd like to find a better way of selecting color support.

As of right now, I just check the value output by the tput colors command. If that gives a value other than 8 or 256, I check the TERM environment variable against a list of known TERM values to determine if it's a 256 or 8-color shell. If the TERM is unknown, default to no color.

// Convert the TERM variable to lower
switch strings.ToLower(term) {

// 256 colors
case "xterm":
case "rxvt-unicode":
case "rxvt-unicode-256color":
LoadColors256(colors)

// 8 colors
case "ansi":
case "rxvt":
case "linux":
case "vs100":
case "vt220":
LoadColors8(colors)

default:
// Otherwise, load no colors
}

After which, I load a map (dictionary) with up to 256 values that can fetch hex color codes via a color name, then output those colors formatted in RGB later (i.e. '\\033[X8;2;R;G;Bm').

I know this system is very flawed, so I ask:
Is there something else I should be doing?

https://redd.it/1btqkmv
@r_bash
I need a noscript

I am trying to write a bash noscript that will search for MAC address pattern in a file. Any suggestions?

https://redd.it/1btqmz7
@r_bash
Integer not incrementing correctly (help a bash newb)

Hi guys, made this bash noscript to record a radio show every week, I use id3v2 to insert the metadata / track number but it doesn't seem to wanna work, if someone could tell me what I'm doing wrong I'd greatly appreciate that.

RECORDS_COUNT=$(ls -l $COUNT_PATH | grep \^- | wc -l )
((RECORDS_COUNT+=1))

For some reason RECORDS_COUNT doesn't increment anymore, it only displays the number of files in the path and the +1 doesn't seem to take effect.

​

https://redd.it/1btt9rw
@r_bash
A noscript to automatically rename music folders

Hi!

I'm doing some cleaning in my local music files and I would like to change the way the folders of my albums are named. Currently it is Album name (YYYY), "YYYY" being the release year of the album ; and I would like to change that for : YYYY_Album name

But since I have thousands of albums, I think it would be quite long to do this manually 😬, so I thought about writing a noscript. The only problem is... I do not know how to write a noscript 😭😁

Could you help me to do this ?
🪴

https://redd.it/1bu34ld
@r_bash
This media is not supported in your browser
VIEW IN TELEGRAM
A Bash Script To Display Animated Christmas Tree In Terminal
https://redd.it/1bu7ytn
@r_bash
Conditional pipe? or a command that can conditionally pipe data

Got this command in .xinitrc:

xinput list | grep -oP '(AT Translated Set 2 keyboard|DualPoint Stick)\s+id=\K\d+' | xargs -n1 xinput disable

I sometime switch between computers, and in one of theme the keyboard is faulty so I tend to run this to disable it; but it also runs on the non-faulty computer;

I was wondering if there was a way (beside storing in a variable then check) to run xinput disable on condition that stdin has two lines? (one line to match keyboard's ID and one to match the dualpoint's ID?)

there probably is a simpler way, like checking before starting to pipe, but this is a pattern I run into quiet often and would love to know if there is a way to solve it;

https://redd.it/1bus1fw
@r_bash
New to coding

Sorry this maybe the stupidest question any of you have read. But is it post to write a bash noscript that will run in a Windows OS.

https://redd.it/1bv7nq7
@r_bash
blog Journey of disabling filename expansion for Bash alias

I wanted to disable Bash pathname expansion for my Bash alias:

alias g='git'

This ended up being not trivial, as there was trouble lot of different trouble with pipes/stdin. In the end I managed to find working solution and wrote blog post about the steps leading to it:

https://miropalmu.github.io/homepage/bash\_noglob\_for\_alias.html

TLDL:

alias g='pstash galias; set -o noglob; consume_noglob_and_pstash galias git'

where the definitions of the Bash function pstash and consume_noglob_and_pstash can be found at end of the blog post.









https://redd.it/1bvs0rd
@r_bash
bash noscript to organize files based off the file extensions.

Trying to organize my files a little bit. How would I go about writing out this noscript?

https://redd.it/1bw15pq
@r_bash
Copy/backup directories inside multiple directories

I'm making a noscript to config my setup, It install some packages and copy my cloned .config/ and .bashrc to my home folder, originally this only happened with the current user where I had run the noscript, but then I wanted this to happen for all users' home folder (or for all users in home?), I was successful in copying the files and direc to all user homes with these commands:

echo /home/*/ | xargs -n 1 cp ~/dots/.bashrc
echo /home/*/ | xargs -n 1 cp ~/dots/.config/

But before the noscript does this, I want it to make a "backup" of current .config/ and bashrc off each created users, I thought something like this would work, but that's not the case:

#create .oldconfig/ for each user
echo /home/*/ | xargs -i mkdir {}.oldconfig/
#move the current .config/ and .bashrc From all user to each .oldconfig/ inside their home directory
echo /home/*/ | xargs -i mv # ... incomplete

I tried using this Xtendedargs instead of some loop, as it should be done in a single command (if it worked)

https://redd.it/1bx0s1q
@r_bash
Where Can I Find Well-Crafted Code?

I understand the importance of learning from bash noscripts written by others, but I want to avoid picking up bad habits or inefficient techniques.

Could anyone recommend a website or resource where I can find high-quality, real-world bash noscripts—not just examples?

Thanks for your help!

https://redd.it/1bx8mci
@r_bash
Getting information about a specific process using process id

How exactly would I go about achieving this in bash?

https://redd.it/1bx9n8a
@r_bash
A useful yet simple noscript to search simultaneously on mutliple Search Engines.

I was too lazy to create this noscript till today, but now that I have, I am sharing it with you.

I often have to search for groceries & electronics on different sites to compare where I can get the best deal, so I created this noscript which can search for a keyword on multiple websites.

# please give the noscript permissions to run before you try and run it by doing
$ chmod 700 noscriptname
#!/bin/bash

# Check if an argument is provided
if $# -eq 0 ; then
echo "Usage: $0 <keyword>"
exit 1
fi

keyword="$1"

firefox -new-tab "https://www.google.com/search?q=$keyword"
firefox -new-tab "https://www.bing.com/search?q=$keyword"
firefox -new-tab "https://duckduckgo.com/$keyword"

# a good way of finding where you should place the $keyboard variable is to just type some random word into the website you want to create the above syntax for and just go "haha" and after you search it, you replace the "haha" part by $keyword

This noscript will search for a keyword on Google, Bing and Duckduckgo. You can play around and create similar noscripts with custom websites, plus, if you add a shortcut to the Menu on Linux, you can easily seach from the menubar itself. So yeah, can be pretty useful!

Step 1: Save the bash noscript Step 2: Give the noscript execution permissions by doing chmod 700 noscript_name on terminal. Step 3: Open the terminal and ./noscriptname "keyword" (you must enclose the search query with "" if it exceeds more than one word)

After doing this firefox must have opened multiple tabs with search engines searching for the same keyword.

Now, if you want to search from the menu bar, here's a pictorial tutorial for that
Could not post videos, here's the full version: https://imgur.com/a/bfFIvSR


https://preview.redd.it/fbw7y9u4tusc1.png?width=717&format=png&auto=webp&s=bbc5b252419683f1ecf333fffbd389d9edfd16cd

&#x200B;

https://preview.redd.it/my994k3ktusc1.png?width=714&format=png&auto=webp&s=9e46fa2c059d56351edf965a7f159edf35cdee88

&#x200B;

copy this, !s basically is a unique identifier which tells the computer that you want to search. syntax for search would be: !s\[whitespace\keyword](https://preview.redd.it/j872qczktusc1.png?width=714&format=png&auto=webp&s=bce94396e4e03c9327de124eedf121b6c554628b)

If your search query exceeds one word use syntax: !s[whitespace]"keywords"

&#x200B;

https://preview.redd.it/j294497mtusc1.png?width=1667&format=png&auto=webp&s=a00d4340b7ad958fbdf577367170c07fcd36248f

https://redd.it/1bxamwp
@r_bash
A small app for coloring text

I didn't know if this was a good subreddit to submit this because it seems to be more focused on bash noscripting, but this app was made with bash noscript development in mind. If it doesn't belong here, please let me know.

&#x200B;

I made an app a few days ago that makes it easier to style text with ANSI escape codes called Gecko.

It uses a flavor of markup tags found in Spectre.Console. So if you wanted to change the color of text, you would simply use:

gecko "cyan1Hello, World!"

The tag to reset color is [/], and unlike Spectre.Console, can be used anywhere.

More information can be found at the GitHub repository: https://github.com/ScripturaOpus/ChameleonTerminal


I'm mostly looking for people to abuse this app so that I can find bugs, but also as a regular release for people to use.

Let me know if it can be useful and what else to add!

https://redd.it/1bxmcad
@r_bash
A noscript to rename folders

Hi! I have posted this : https://www.reddit.com/r/bash/comments/1bu34ld/a\_noscript\_to\_automatically\_rename\_music\_folders/?utm\_source=share&utm\_medium=web3x&utm\_name=web3xcss&utm\_term=1&utm\_content=share\_button

I want to rename all of the albums's folders of my music library like : Music/Artist/Album (YYYY)/ --> Music/Artist/YYYY_Album/

'YYYY' is the year of release year of the album.

I have now the following noscript :

#!/bin/bash

for dir in //;do
[[ $dir =~ (.
)\ \(([:digit:]{4})\)/$ ]] &&
echo "${BASHREMATCH[0]}" "${BASHREMATCH2}${BASHREMATCH1% }/"
done#!/bin/bash

for dir in //;do
[[ $dir =~ (.
)\ \(([:digit:]{4})\)/$ ]] &&
echo "${BASHREMATCH[0]}" "${BASHREMATCH2}${BASHREMATCH1% }/"
done

But it renames from : Music/Artist/Album (YYYY)/ to : Music/YYYY_Artist/Album/

What can I change to get the folders named like : Music/Artist/YYYY_Album/, i.e. YYYY_ to be set before the album's name and not before the artist's?

https://redd.it/1by4w09
@r_bash
Can you use GNU grep to check if a file is binary, in a fast and robust way?

In another thread, someone mentioned that neofetch is written in bash. I did not know that, so I made a small noscript to check what interpreters are being used by the executable files in my `$PATH`.
The main problem is testing if the file is text or binary. I found this 10-year-old discussion on Stack Overflow: https://stackoverflow.com/questions/16760378/how-to-check-if-a-file-is-binary

Anyway, here is my noscript:

#!/bin/bash
time for f in ${PATH//:/\/* }
do
[[ -f $f ]] &&

#checking if file is binary or noscript, some improvement would be nice
head -c 1024 "$f" | grep -qIF "" &&
value=$(awk 'NR==1 && /bash/ {printf "\033[1;32m%s is bash\033",FILENAME }
NR==1 && /\/sh/ {printf "\033[1;35m%s\033[0m is shell",FILENAME}
NR==1 && /python/ {printf "\033[1;33m%s\033[0m is python",FILENAME }
NR==1 && /perl/ {printf "\033[1;34m%s\033[0m is perl",FILENAME}
NR==1 && /ruby/ {printf "\033[31m%s\033[0m is ruby",FILENAME}
NR==1 && /awk/ {printf "\033[36m%s\033[0m is awk",FILENAME}' "$f")
[[ $value = *[[:print:]]* ]] && arr+=("$value"); unset value
#I first assign file to a $value because if I would have sent it directly to the the array, a '\n' would be added to `arr[]` if awk evalutes to nothing.
#for example, if the file would be written in a language not mentioned in the awk program, like lua, awk would return nothing and then arr+=('\n').
done

files=$(fzf --multi --ansi <<<"${arr[@]/%/$'\n'}" | cut -d " " -f 2) #f2 cause the first field is the ansi escape code for fzf, I guess...
#shellcheck disable=SC2086
[[ $files ]] && "${VISUAL:-${EDITOR:-cat}}" ${files/$'\n'/\ }


Any way to make it faster and more robust?
The idea behind it is to type in fzf `is\ bash` `is\ perl` `is\ shell` `is\ python` to see the numbers of noscripts you have for each language in your PATH and if you want multi-select the noscripts you want to read the source code in your EDITOR of choice, or it will be printed on the terminal via `cat`

https://redd.it/1byfsce
@r_bash
why shall IFS be set to "\n\b" and not just "\n" to work with space containing filenames ?

Hi all,

I don't get why simply setting IFS to "\\n" doesn't work .

let's say I have 2 files in the current directory named "big banana" and "huge apple" and "small orange"

$ IFS=$( echo -ne "\n" )

for i in $(ls *);

do echo "file: $i";

done

It doesn't work, the output is :


file: big banana

huge apple

small orange

instead of

file: big banana

file: huge apple

file: small orange



As a matter of fact it works if IFS is set the following way:

$ IFS=$( echo -ne "\n\b" )

Does someone know why \\b is also needed in IFS definition?

Thanks for your help

https://redd.it/1byha3q
@r_bash
Why won't it log ps -p?

read -r -p "Enter process name: " cpid
apid=$(pgrep "$cpid")
ps -p "$apid"
read -r -p "Log process yes/no " log
if [ $log == "yes" ]
then
ps -p "$apid" >> pslog.txt # this is where it fails

This is what I get when I run the noscript:

https://preview.redd.it/vpxc65zn9btc1.png?width=444&format=png&auto=webp&s=d7602ecad132d3ee980f6462a0c72b25f86a4d62

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