Guidance with bash shell scripting?

ArchieLinux

New Member
Joined
Jun 10, 2026
Messages
3
Reaction score
6
Credits
53
Greetings! Long time Arch/EOS user but brand new to this forum. I've got a bash scripting challenge that I cannot readily solve...

Specifics: Currently, within the ranger file manager, I can use a shell script to launch an email app (in message composition mode) with a single attachment. But I'd like to be able to add multiple attachments (selected within ranger) to an email composition. The email app is thunderbird or betterbird (same difference).

Here's what I've already got that successfully launches the email app in composition mode with a single selected file as the attachment:

:shell betterbird -compose "attachment=%s"

This works perfectly ... but if multiple file selections are made the email app chokes, reporting that the string provided by %s is not acceptable.

This %s parameter seen above is a text string provided by ranger that includes however many currently selected files there are within the file manager. But the email app cannot interpret the list produced. Apparently, it needs to be , (comma) separated with no intervening spaces as discussed below.

From Mozilla's CLI documentation for thunderbird (of which betterbird is a cloned fork with same specs): https://kb.mozillazine.org/Command_line_arguments_-_Thunderbird

1781129539601.png


So... what I have been unable to figure out is how to parse the %s string to eliminate any spaces and place , separators between the names of each selected file.

Familiarity with ranger and its "macro strings" might be helpful background to solve this.

FWIW, I've got similar functionality working perfectly in nnn (another TUI interface file manager I've been using for 7 years or so). But the nnn bash script solution operates differently enough that it isn't helping me solve this one.

Since ranger is written in python, another possibility might be to craft a plugin python script, but I haven't wandered into that forest yet. ;-)

I've tried a number of approaches, so far without success. To anyone who sees what I'm trying to accomplish and has some ideas on how I can proceed, I'd sure appreciate hearing your thoughts!
 


I understand that ranger passes arguments to bash script by calling it and giving arguments to it on command line? in same way how human would call a script with args.

If yes, then you'll need to write a loop in the script that parses arguments and constructs a valid command line prior calling betterbird.

You can't directly pass a string of selected items from ranger into email CLI, ranger knows nothing about the format you need that's up to you to adjust.

Bash:
# Parse args and construct attachments cmd line
CMD_LINE="to='[email protected],[email protected]'"
ATTACHMENTS=",attachment=" # note the comma

for arg in "$@"; do
   ATTACHMENTS+="'$arg," # note the comma and '
   ATTACHMENTS+="'," # append the comma and '
done

# Append and remove last comma
CMD_LINE+=$ATTACHMENTS | sed 's/.\{1\}$//'

# Use cmd line
thunderbird -compose "$CMD_LINE"

I haven't tested this btw.
 
NOTE: THE FOLLOWING NEEDS TO BE FURTHER EDITED TO ENSURE IT WORKS WITH FILENAMES THAT INCLUDE SPACES ...

For users of ranger file manager, I wanted to follow up as to how I utilized ranger's in-the-box %s macro (which passes either the name of a highlighted file or the names of a series of selected files - via the space bar) as actual file attachments to a new email opened up in composition mode.

There are some quirks to work around, and the following approach works perfectly... but I imagine it can be streamlined and simplified further given additional effort [Note: the sed command seen below is needed to replace " " (spaces between filenames) with "," (commas between filenames). While ranger's %s parameter produces a space-separated list, both of these email clients I use require comma-separated filenames when referring to attachments in composition mode.] :

Within ranger's rc.conf configuration file (assuming it's in ~/.config/ranger directory), add this line (to map to ee key sequence):
map ee shell echo %s > dummy ; sed -i 's/\ /,/g' dummy ; ~/.config/ranger/bb_att.sh < dummy

With the following bash script as bb_att.sh:

#!/bin/bash​

read arg​

betterbird -compose "attachment='$arg'"​

rm dummy​


This is for using betterbird email client. For thunderbird simply change to:

#!/bin/bash​

read arg​

thunderbird -compose "attachment='$arg'"​

rm dummy​


I've tried passing ranger's %s text list of space-separated selected files within the bb_att.sh itself, but it's a swing and miss. Perhaps there's an xargs solution possible, but I haven't quite hit on the precise syntax that makes it all work.

At least this admittedly convoluted approach is functional. If a single file is highlighted within ranger then hitting ee works. If multiple files have been selected using the spacebar to mark them, it also works.

Either way, ranger opens up the email client with the respective file(s) as attachments, ready for email composition. After sending it, you return to ranger again.

Hopefully this might be of help to someone else down the road!

Thanks again to @CaffeineAddict who got me thinking down the right path.
 
Last edited:


Follow Linux.org

Staff online

Members online


Latest posts

Top