r_bash | Unsorted

Telegram-канал r_bash - r_bash

44

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

How to use part of path as a variable in command?

I started in Linux 4noobs. It was suggested I try here.

I need to clean up the metadata of my music collection. Specifically, I need to add an albumartist tag to all of the files. Ideally, I would prefer to only add them to files that do not already have them.

On an artist by artist basis, I can do this manually.

find ./Soapy\ Argyle/ -type f -name "*.flac" -exec metaflac --set-tag="ALBUMARTIST=Soapy Argyle" {} +

But given the size of my collection, that would be burdensome.

Collection is organized ./Music/Genre/Artist/Album/Disk#/song.flac

How can I grab the artist name from the path and use it in the command above, or do something functionally similar?

As a bonus, this lists files that already have albumartist tagged.

find ./ -type f -name "*.flac" -exec metaflac --list {} + | grep -E -i 'ALBUM[[:space:]]*ARTIST'

Can I mesh these things together so I don't end up with a bunch of songs double tagged?

Thanks for any suggestions.

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

Читать полностью…

r_bash

bash script to find and change directory

Hello All,



I’m not a bash programmer at all but I want to write a bash script which does the following,

First, determine which "Downloads" folders is present.

The English one"Downloads" , or the French "Téléchargements" one.

The username is unknown so must be determined also as a variable.

$USER if not mistaking ?

After determining the "Downloads" folder, I want to set the complete folder path as a variable.

I’m sure it’s possible but after trying several times, I can’t get it right.



Can you please help me?

Thank You very kindly,



Tom

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

Читать полностью…

r_bash

Trying to copy multiple directories using aws cli from bucket to local disk

In a S3 bucket, I have a number of directories I need to copy from a S3 bucket to a local disk.

The directories have a date naming pattern like:

2025-12-10/

2025-12-11/

2025-12-12/

2025-12-13/

2025-12-14/

2025-12-15/

I have access to the S3 bucket and was trying to automate the copy process



$ for i in $("2025-12-10") ; do mkdir $i && aws s3 cp --recursive s3://<s3-bucket-url>/$i ./$i ; done



What am I doing wrong here?

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

Читать полностью…

r_bash

ze.sh: frecency-based file tracking alongside directory jumping
/r/zsh/comments/1w0pwob/zesh_frecencybased_file_tracking_alongside/

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

Читать полностью…

r_bash

Bash scripting newbie here, wanting to improve/tidy one of my backup scripts (entirely for personal use)

Please excuse the vague thread title, I'm a newbie enough that I'm not sure what's possible / desirable etc, nor have I attempted functions in bash scripting yet (I've played with them in VBScript and PowerShell IIRC).

One of the backups I do produces date-stamped tar.xz files of particular folder structures on my computer and transfers them to a veracrypted drive. Today I had a crack at a new script based on this one that mounts the filesystem (sshfs) on my laptop and transfers the files over the network after they've been compressed.

I run the script with up to 4 arguments, e.g. 'essentials' 'archive' 'paperwork'. In the script there's an if statement for each potential argument, e.g.:

for arg in "$@"
do
if $arg = "essentials" ; then
tar -cf - .mozilla/ | pv -s $(du -sb .mozilla/ | awk '{print $1}') | xz -T0 > /tmp/firefox-$datestring.tar.xz
rsync -ah --progress /tmp/firefox-$datestring.tar.xz /media/mikelpmintfs/firefox-$datestring.tar.xz
fi
<more if $arg = whatev then compress and transfer stuff statements here>
done

(btw the whole fancy progress bar bit with pv -s and awk was something I copied off the Internet)

The first script also included some error catching, e.g.:

if $? -eq 0
then
echo "archive backup complete."
else
echo "error performing archive backup" >&2
fi

I've used if <command here> then else fi before too, but I'm wondering multiple things:

1. Rather than writing each compression command and each rsync transfer command per argument, would it make more sense to write a function, or given that some of these source folders are in completely different places in my computer's file system, is this worth it.
2. error trapping: On one hand I think that the script could easily trip up at the compression or transfer stages, but I'm worried about over-nesting if statements and making the whole thing a lot harder to read and figure out where something is going wrong. It seems to me that it could be function'd up, but would it actually help with readability etc. When the script is just for me, I can tell if it went wrong if I get a load of unexpected output :)

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

Читать полностью…

r_bash

Lbuiltin - my Bash builtin guilty pleasure

Hello. I created [L\
builtin](https://github.com/Kamilcuk/Lbuiltin) \- one Bash builtin bundling multiple subcommands together into one.

While working in Bash and particularly on my [L\
lib](https://github.com/Kamilcuk/Llib) library I noticed a lot of really small, but annoying things missing in Bash. Syscalls missing, like pipe or seek. For a long time, years, long before AIs, I really wanted to write a Bash builtin. However, going through Bash source code would be tedious work to understand all the tiny details that Bash works internally to actually write anything usable. I never got the time. But I got some now to write a prompt and then tinker with the result to make it usable. It comes with so much I could think of:

* `L
builtin pipe VAR; echo >&${VAR0} for opening a pipe.
*
Lbuiltin lseek -v pos 3 1024 CUR` for seeking a file descriptor. No more python or perl!
* Network: `L
butilin listen/accept/connect/shutdown. Because let's face it, what we really wanted is to write a [web server in Bash](https://github.com/Kamilcuk/L_builtin/blob/main/scripts/chatserver.sh#L159).
*
Lbuiltin memfd FD; echo data >&$FD` in case you want a real temporary file descriptor. And you can seek it. With `Lbuiltin lseek.
*
Lbuiltin epoll create efd` and `poll` and `ppoll`, because event loops in Bash is a must.
* `L
builtin signalfd timerfd eventfd for for real work with polling above.
*
Lbuiltin read -f hex` because we have to be able to read a zero byte from a file descriptor.
* `L
builtiln sig block/unblock for blocking and unblocking signals. Finally receiving SIGINT in interactive Bash shell.
*
Lbuiltlin sedvar VAR 's/a/b/'` to run full sed over variable value, because `${//` is not enough.
* `L
builtlin core ls/sleep/... includes some Rust coreutils. Just to showcase it is possible to have them all in Bash as builtins.
*
Lbuiltin ext ...` has \~50 builtins compiled from Bash source code from examples/loadables directory. Csv parsing, sorting bash in place, some common utils like basename and chmod.

And finally my pleasure: Bash inter process synchronization! Mutex! Barrier! Semaphore! And... process shared variable! Consider this:

L
builtin shm bind VAR; VAR=1
( VAR=2 ) &
wait
echo "$VAR"
# Outputs 2!

Imagine this? Now possible. And works. All works by keeping state in a memfd_create file descriptor shared between processes. So much fun. Imagine a process parallel quicksort in Bash.

The builtin is re-re-compiled for multiple versions of Bash and bundled together and it picks proper version on runtime and dlopens it. So it works seamlessly with any Bash version. I compile for 4.4, 5.0, 5.1, 5.2 and 5.3. I tested in some docker images and it works on any of them provided they have compatible enough glibc. It should work with any Bash 4.4+ on any modern-ish Linux. Install with:

mkdir -vp ~/.local/lib/bash/
wget -O ~/.local/lib/bash/Lbuiltin.so https://github.com/Kamilcuk/Lbuiltin/releases/latest/download/Lbuiltin.so
enable -f ~/.local/lib/bash/
Lbuiltin.so Lbuiltin
L
builtin --help

Subprocess shared variables and barriers and mutexes in Bash are insane. I have spent some time writing this builtin. I wonder if there is reason to invest in it more. I have a lot of ideas for even more improvements - make the shared memory database an LMDB for super speed, reduce library size by introducing uniform API abstractions over Bash, allow assigning arrays like printf does with -v 'arr[idx]. And fixing docs in many places with more examples.

Anyway, it works. I guess have fun with it if you want. Also it is in Rust. Thanks.

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

Читать полностью…

r_bash

Search for a utf16 string within a process' memory and return its address

Hiiii

How do you search for a utf16 string within a process' memory and return its address?

I tried various things with gdb, dd, grep, whatever, I've tried like a million things and I've gotten a million different issues, so just, how do I do it? I have gotten to the point of having a loop that gives me the beginnings and ends of readable segments from /proc/pid/maps, but Idk what to do with that

Current conclusion: bash is just a bad language for this?

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

Читать полностью…

r_bash

Universal Linux CUDA SDK Toolkit auto-installer script

This script makes installing the CUDA SDK Toolkit easy on any of the supported Linux distros. It detects the latest stable version available and installs it without any interaction needed. It sources everything directly from https://developer.nvidia.com/cuda-downloads.

If anyone experiences any issues just let me know and I will correct it.

You can find the script on GitHub.

Cheers

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

Читать полностью…

r_bash

What is a clean Bash pattern for resuming a partially completed batch?

Suppose a Bash script walks thousands of independent inputs and may be interrupted after some outputs have been written. Skipping every existing output is unsafe because a truncated file also exists, while restarting the whole batch wastes work and can repeat external side effects.

A pattern I am considering is to write each result to a temporary file in the destination directory, validate it, rename it atomically, and then append the input ID plus an output hash to a journal. On restart, the script would trust only journal entries whose current hash still matches. A lock would prevent overlapping runs, and traps would clean only the current process's temporary files.

Where does this pattern fail in Bash, especially with parallel workers, NFS, or a crash between the rename and journal append? Is there a simpler checkpoint design that remains understandable without turning the script into a database application?

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

Читать полностью…

r_bash

My first Bash Script

Hello, I started learning Bash 2 years ago but I didn't really tried to learn it in this 2 years. I actually started using Linux 2 years ago but never tried Bash. 1 or 2 weeks ago I tried writing my first if statement, after that I tried learning more. This is my first project, transfer part is still not yet done but it's mostly over so that's why I wanted to post this. I'd be very happy if anyone could tell me how to make this better.


https://github.com/molbegigib-ops/MemiBank

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

Читать полностью…

r_bash

One of the big things that bugs me in bash is how to write those boolean check_, exists_ type of functions, any standard practices?

```
#!/usr/bin/env bash

function is_existing_docker_network() {
local -r debug="${1:-false}"
local -r network_name="$2"

if docker network inspect "${network_name}" >/dev/null 2>&1; then
[[ "${debug}" = "true" ]] && log_warn "docker network ${network_name} already exists"
return 1
else
return 0
fi
}

function create_docker_network() {
local -r debug="${1:-false}"
local -r network_name="$2"

[[ "${debug}" = "true" ]] && log_info "creating docker network ${network_name}..."

if docker network create "${network_name}"; then
[[ "${debug}" = "true" ]] && log_info "docker network ${network_name} creation completed"
return 0
else
log_error "docker network ${network_name} creation failed"
return 1
fi
}
```

- Look at the create_docker_network function above
- I want to create a network only if it doesnt already exist
- In a normal programming language you would simply return a true or false from the is_existing_docker_network function above
- How do you go about do this kinda thing in bash? Should I actually return 1 if a network exists? Isnt that an error condition?

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

Читать полностью…

r_bash

will someone explain to me getopt?

yes, I am currently reading the documentation - I don't understand the point or when/why I would use it. thanks for any clarification one can offer.

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

Читать полностью…

r_bash

Lbashprofile compare

Hello. Some time ago I added a "compare" subcommand into L\_bash\_profile. I also discovered that with qemu it is possible to count the exact number of instructions executed by a program. Thus now I am able to micro-optimize bash with the number of instructions executed. Below I can share some findings.

How do I use it, I pick two expression to compare given the same input conditions. For example which method is faster to iterate over arguments, is it while (($#)); do ...; shift; done or for i in "$@"? For both expression I define input for example -P 'set -- 1 2 3' will add set -- 1 2 3 to both expression in front and will not count instructions of it. So the following:

$ Lbashprofile compare -m qemu -P 'set -- 1 2 3 4 5 6 6 7 8 9 10' \
'while (($#)); do : "$1"; shift; done' \
'for i in "$@"; do : "$i"; done'
Benchmarking 1/2: 'while (($#)); do : "$1"; shift; done'
Benchmarking 2/2: 'for i in "$@"; do : "$i"; done'
Comparison results (method: QEMU, repeat: 1):
| Code | ExitCode | Insn | ΔInsn |
|--------------------------------------|------------|--------|---------|
| while (($#)); do : "$1"; shift; done | 0 | 251562 | - |
| for i in "$@"; do : "$i"; done | 0 | 167719 | -83843 |

From that I see that for is faster. Usually I write a script with funcitons and do L_bash_profile compare -P '. script' 'func1' 'func2' to compare two functions defined in some temporary script.

Reduce the number of expressions. Literally reduce. You now you can assign variables in one line? Like with local, but no local. Instead of a=1; b=2 you can in one line a=1 b=2 for the same effect. Use arithmetic expansion with side-effects. Instead of a=1; b=2; c=$(( a * b )) you can do (( a = 1, b = 2, c = a * b )).

Fastest way to represent booleans is (( var )) and storing var=1 or var=0. It is faster then storing true or false and doing if $var; then and is faster then [[ $var ]] or [[ $var = true ]] or similar. Overall arithmetic expansion is fast.

Case is fast. Whenever you want to compare strings or want to write [[ string == <glob> || string == <another glob> ]] do a case. Case is optimized, i.e. pattern* will just check prefix, *) will not even compare. You can optimize more, for example instead of if (( bool1 && bool2 )).... elif (( !bool1 && bool2 )) etc. you can do one case "$bool1:$bool2" in 0:0) ... 1:1) ... 1:0) ... etc.

Extglob is slow. Regex is also slow. Glob is fast. Doing string replacements with extglob with big strings is extremely slow. If glob is not enough, use regex, and more often then not one big regex can be used instead of multiple extglob. But overall, use glob whenever you can. One regex is worth more then 10 globs. If you can do something in one regex [[ $var =~ and in about ten globs var=${var##<glob>}; var=${var%%<glob>} var=${var//<pat>/<sub>} use globs. Globs are fast.

For example, to trim leading spaces from a string, using double glob var="${var#"${var%%[![:space:]]*}"}" is twice as fast as var=${var##+([[:space:]])}. That is because extglob is O(n^2) - it tries to match every position in the string going over and over it in a loop.

Do not copy variables. Might sound obvious, but want to mention one thing. It is funny Bash has a "implicit outer scope" of variables, in contrast to other languages. Instead of local args=("$@"); some_function "${args[@]}" refactor and do local global_args=("$@") global_args_index=0; some_function_that_works_with_global_args and just use global_args and global_args_index directly in functions working on one global array. Pushing values into $1 $2 into every function is creating copies and copies everywhere.

Overall, when parsing arguments, shift is slow. Or maybe not shift itself, but the way you build the loop

Читать полностью…

r_bash

Find command confusion between ; and \;

Why do we have to use '\\' instead of leaving it out or even ';'? I read the find commands' manual and it says '\\' might be needed to protect from expansion by the shell. So what is expansion also?

What is difference between \\; or ; or leaving it out?
What is shell expansion here?

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

Читать полностью…

r_bash

I created a modular tycoon in Bash

I created a tycoon in Bash, but it’s not your typical tycoon -it’s modular. That means there’s no upgrading droppers or any of that fluff; instead, you build the droppers yourself using any programming language (or so I assume).

There are two versions of this tycoon on my GitHub: one with a C# module and one without (if the C# module doesn't work quite right, please don't beat me with sticks).

It’s currently in alpha, so there are likely bugs (especially since Bash isn't exactly a language for game development).

If you're curious, give this "quick-and-dirty" coding masterpiece a try - here’s the link to my GitHub: https://github.com/mishkagumiberr-star/modular\_tycoon

Good luck! :)

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

Читать полностью…

r_bash

reedline-bash - using nushell's reedline (line editor) in bash.

https://i.redd.it/fgc5uoy60jnh1.gif

I made a bash plugin that replaces bash's line-editor, readline, with nushell's reedline.
This brings all the line-editing, styling, completion, keybind and selection features from nushell to native bash. It's still pretty early in development, most reedline features should work just fine, but things may still break :)

repository

code partially written with AI (claude).

Credits:
\- flyline \- a similar project that implements a custom line-editor, i used this as main reference for replacing readline
\- reedline

GIF created with https://github.com/HalFrgrd/evp

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

Читать полностью…

r_bash

Tips for Speeding Up Your Bash Scripts

I wrote a script that renames a lot of files (tens of thousands). Performance was kind of slow (over 50 s), but with two changes it now runs at under 3 seconds, a 20x speed boost. Thought I'd share, in case it helps anyone else. These tips are good if you do things thousands of time in a script, I don't think they are relevant in all scripts.

Don't spawn sub shells if you can avoid it

Instead of command substitution with printf:

new_file="$(printf "%s/%s_%.${len}d.%s" "$new_dir" "$prefix" "$i" "$ext")"

do:

printf -v new_file "%s/%s_%.${len}d.%s" "$new_dir" "$prefix" "$i" "$ext"

This will save a sub shell.

Use builtins instead of external programs

Bash has optional builtins that you can enable. On my system they are located in /usr/lib/bash . You can enable them in your script by using enable <builtin>. Be sure to check the exit code. On my system a mv builtin is not available, but since I was renaming on the same file system I figured I could use ln and rm instead.

So instead of using external mv for each file I did:

builtin ln "$old_file" "$new_file" && builtin rm "$old_file"

In this case the builtin probably isn't required since builtins are prioritized over external commands, but I used them to be more explicit. Just be sure to use && so that rm never runs unless the hard link has been successfully created.

EDIT: The part about using ln and rm builtins may be a little too much of a hack. Probably better to use proper tools like rename to do batch renaming, as was pointed out in the comments. Also keep in mind that the builtins are more bare-bones than the proper CoreUtils programs. For example rmdir does not support -v and will treat -- as a directory to remove rather than "end of options-thing".

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

Читать полностью…

r_bash

Get file names only with a glob

I want to loop over file names not whole paths in a folder so I tried for file in $(ls folder) but if the folder doesn't exist the script doesn't fail because it's in $(). I can use for file in folder/* which fails if I add failglob but that returns the whole file name so I have to use basename for each of them. Can I get just the names from a glob directly?

bashrc isn't used in bash scripts. Is there a file where I can put settings like shopt -s failglob and set -o pipefail that applies to all my scripts so it is still used if I forget to add it to one of them?

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

Читать полностью…

r_bash

For some reason, when I run this script, it always plays the same song.
https://redd.it/1wyhejw
@r_bash

Читать полностью…

r_bash

Looking for help with a script

Hey guys,

On my steamdeck I used a script to rotate the screen. Didnt write this myself but got it off of github. Since then the steamdeck has switched from X11 to Wayland, making the script inoperable because it relied on xrandr.

I rewrote the script to use kscreen-doctor instead. Figured out the necessary commands and values and rewrote the script. I'll past it here.

#!/bin/bash

screen="eDP-1"
defaultscreenorientation=8

screeninfo=$(kscreen-doctor --o | grep "$screen" | awk '/Rotation/ {print $3}')

if [[ "$screen
info" -eq "$defaultscreenorientation" ]]; then
kscreen-doctor output.eDP-1.rotation.right
else
kscreen-doctor output.eDP-1.rotation.none
fi

The issue im running into is as follows: defining the value of screen_info returns null. When I run the command seperately in the terminal it returns "8" as expected but within the script it doesnt.

I have absolutely 0 experience in coding apart from some very short scripts in Stationeers. Can you guys help me figure out whats wrong? Or at least point me in a direction for me to find out whats wrong?

In Excel you can let a formula run step by step, does something like that exist for bash?

Anyhow, many thanks in advance for reading my post!

EDIT: Thanks to the comments below I've made some adjustments. The only thing that was needed was to remove the grep. This fixed the issue of not returning a value.

Now Im running into the next issue, the if statement doesnt work consistently. If I format it as follows:

if [ "$screen_info" == "$default_screen_orientation" ]; then

It only sees the values as not equal. When the values are the same it still returns a not true.

If I format it as:

If ((screeninfo=defaultscreenorientation)); then

It only sees the values as equal. When the values differ it still returns a true.

Any pointers for this?


FIXED: the value coming from awk was not a true numeric value but one with formatting. I added the following after awk: | grep -o '[1-8\]' )

Not the cleanest solution but it works.

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

Читать полностью…

r_bash

a safer rm implementation.

del() {
local trash="$HOME/temp/trash"
[ ! -d "$trash" ] && {
printf "trash folder not exist, created one;\n";
mkdir -p "$trash";
}
(( $# < 1 )) && { printf "Include an object to delete\n"; return 1; }
for arg in $@; do
if [ "$1" = -* ]; then
case "$1" in
-show) ls -a --color "$trash" ;;
-clear) rm -fr "$trash"/ ;;
) echo "flag not fount"; return 1 ;;
esac
return 0
fi
local object="$arg"
[ ! -e "$object" ] && { printf "object $object not found\n"; return 1; }
mv -i "$object" "$trash"
done
return 0
}

a safer system, what do we think?

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

Читать полностью…

r_bash

Any way to never output root user name/path terminal?

Is there any way via a command/config which never exposes my root path/ root user name(`Users/<mypcusrname>/.../<current_dir>`) whenever any command like: pwd, or any other command that involves path output in the terminal/shell?

Like when ever we hit the command the output always shows `/workspace` or `/<curr_dir>` instead of `Users/<mypcusrname>/.../<curr_dir>`

My intention/idea is to never expose the root path or user name in any of the terminal logs - via some command. Especially since I am running any agent thats running some shell commands.

I am not sure if docker sandbox or pi-agent extensions support that. For pi agent the gandolin sandbox allows it not to execute commands outside the current working dir. But it still exposes the root username/root path of the dir in shell commands or even in shell logs.

Thank you in advance 🙏

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

Читать полностью…

r_bash

RemZero | A bootstrap script
https://redd.it/1wc94qe
@r_bash

Читать полностью…

r_bash

Help running a shell script in apple terminal

Using Composers Desktop Project to edit audio files. I want to run a single command on a list of audio files instead of having to manually do it 100 times. Wondering if there is a way to call upon this list of files in a single command line, rather than creating a shell script with the name of every file. The command line with a single audio file would look like this.

distort average soundfile.wav soundfileoutput.wav

the first two words are the command being run and then the input and output respectively. Hopefully I provided enough info on this but also relatively new to using terminal so let me know if there is anything I should clarify, thanks.

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

Читать полностью…

r_bash

Maybe powerlevel10k-style prompts for bash

I've been happily using powerlevel10k with zsh. If you've never used p10k, it's the prompt a lot of zsh users swear by, and plenty of bash users wish they had it too. The catch: p10k is zsh-only, and it's now in maintenance mode.

So I wrote p11k, a cross-shell prompt engine in Rust inspired by p10k. It doesn't work like Starship. Starship is a program your shell calls while rendering the prompt. p11k sits between your terminal and your shell as a transparent proxy. It starts your shell in its own pty and draws the prompt itself. It's more involved, but it also lets you do some things other prompts can't, like instant prompt. Being a separate engine doesn't make it slow, either. Honestly, when I had p10k and p11k open side by side in two windows, I couldn't tell them apart at all, and neither could a couple of friends who use p10k every day.

It covers most of what p10k does, and the same smart directory truncation. The config is KDL v2 instead of a pile of PS1 escapes, and there's a setup wizard like p10k configure.

It's a personal project, but if you miss powerlevel10k, I'd love for you to check it out.

Heads up: Linux only for now.

Powerlevel11k

Thanks for reading.

p11k with bash

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

Читать полностью…

r_bash

Great things about bash?

If is easy to read bad things about bash (and often with good reason). Clearly a bash script will never look as elegant as, say, Ruby.

But what about great things about bash? I mean, "great", not in the sense of improving over a POSIX shell, but of being elegant and/or comparing well with (some well-known) "real" programming languages. Here are my two favorites

the OR list operator, e.g:

&#8203;

[[ condition ]] || {
actions...
}


(more elegant than an "if not"... conditional)

the ability to assign a command name to a variable, var="command --option1 --option2 ...", then execute the command later with:

&#8203;

$cmd

(unquoted to allow the options to appear as separate words).

What about your favorites?

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

Читать полностью…

r_bash

around shift is built becomes slow, as you end up passing arguments to each function. In many cases it is faster to copy arguments once to an array args=("$@") and have an global shared iterator args_i iterating over args array.

Sparse arrays in bash are represented as a linked list with an additional "last used" element. Each element has a pointer to the next and previous element, and each array element stores the index and value. Additionally "last used" pointer to element is used. When accessing an array of some index, Bash checks which pointer to use to get there faster - from the first, last or last used element - and then traverses the linked list until the element of specified index is found. This opens several preferred array access patterns. Random access is slow. It is fast to iterate over the array - from the back, from the front, from the last used element. If you have some work to do on sparse arrays, prefer to put related data in close-by indexes instead of scattering them.

In many cases, it is possible to use some tricks to speed up execution. For example instead of a boring for ((i=X;i<Y;++I)) to iterate over elements from X to Y in many cases it is possible to use eval ...{$X..$Y}... \- use {X..Y} expansion with prepared quoted code. However, this is not always faster and requires profiling. Eval is slow, it is parsing the expression inside. More often then it might be actually faster to write a boring loop iterating over elements then depending on clever tricks.

However, more often then not, reducing the number of commands is profitable. For example lets take a boring eval "$expr" example. If expression is often empty, this executes eval doing nothing. You can write ${expr:+eval} ${expr:+"$expr"} and execute "nothing" (literally nothing, nothing shows up in set -x) when expr is empty or unset. Knowledge of expansions comes handy, and recent ${var@Q} ${arr@k} ${var@a} might be useful.

Similarly, you can do side effects in arithmetic contexts in array subscripts. So arr[i]=...; i=$((i+1)) is just arr[i++]=...;. But with comma operator you can do anything inside arithmetic context. So following that arr[i++]=...; var=2 can be joined together arr[var = 2, i++]=.... Effective everything can be "squeezed" inside one arithmetic context together. Also this is cheaper eval. Arithmetic expansion first expands the input and then calculates. So you can sum arguments with IFS=+; (( $* )) and similar tricks that work really first.



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

Читать полностью…

r_bash

How to Write Regular Expressions (With a Live Tester)
https://ommais.com/blog/how-to-write-regular-expressions/

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

Читать полностью…

r_bash

I made a package manager in bash!

Its called scrapre. It doesn't have any cryptographic verification so note that (please note that) and there is no repository up for it right now cuz I dont really have much need to host one. You cant find it here. (its codeberg btw)

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

Читать полностью…

r_bash

Made a neat little function out of an alias

Came across a little alias that basically just lists out only the dotfiles in the cwd, and wanted to make it work for any directory, but also gives you a count of how many dotfiles are in that directory if, for some reason you need that, so I turned it into a function.

ldot() {
if "$1" == "-c" ; then
shift
ls -AF "$@" | grep -E '^\.' | wc -l
else
ls -AF "$@" | grep -E '^\.'
fi
}



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

Читать полностью…
Subscribe to a channel