Help Using Basename Script on BBEdit Files

38 views
Skip to first unread message

Bill Kochman

unread,
Jul 17, 2025, 8:46:59 AMJul 17
to bbe...@googlegroups.com
Greetings List,

I have an old shell script which I needed to use today and ran into a problem which I haven’t been able to figure out. I still have the BBEdit help file I wrote for myself years ago, but the instructions don’t seem to be working. I have two versions of the script, one for .txt files, and one for .html files.

I have been trying to use the script to add the file name of each file to the top and bottom of over 700 BBEdit files. However, after dropping the shell script into the Terminal window, and then dragging the selected folder full of files to the Terminal, and then removing the ending space and hitting my return key, I get an error for every single file in the folder which says “no such file or folder found".

I have placed the script both on the desktop, as well as in my Home folder. Likewise, I have done the same for the folder containing the 700+ BBEdit files. But no matter what I do, or where I place the script and folder, I still keep getting the error.
I am not sure what I am doing wrong.

I am currently running Sequoia 15.5 on a 2023 Apple Studio if that is of any help.

Did Apple do something which makes these two scripts no longer work properly, or am I just forgetting how to do this properly?

Below is the contents of both scripts. They are identical except for the fact that one has “html" in it, while the other has “txt" in it.

#!/usr/bin/env bash

directory="$1/*.txt"
for f in $directory
do
echo "Processing $f..."
fullfile="$f"
filename=$(basename "$fullfile")
filename_woext="${filename%.*}"
sed -i '' "s/#BASENAME#/$filename_woext/g" $fullfile
done


#!/usr/bin/env bash

directory="$1/*.html"
for f in $directory
do
echo "Processing $f..."
fullfile="$f"
filename=$(basename "$fullfile")
filename_woext="${filename%.*}"
sed -i '' "s/#BASENAME#/$filename_woext/g" $fullfile
done

Thanks in advance to anyone who can help.

Kind Regards,

Bill Kochman

flet...@cumuli.com

unread,
Jul 17, 2025, 9:53:22 AMJul 17
to bbe...@googlegroups.com
The scripts do seem to work for me with a few test files. You could add "set -x" to the top of the script and it will output all the commands being executed. That might give you an idea what is going wrong.

processtxt.sh
----
#!/usr/bin/env bash

set -x

directory="$1/*.txt"
for f in $directory
do
echo "Processing $f..."
fullfile="$f"
filename=$(basename "$fullfile")
filename_woext="${filename%.*}"
sed -i '' "s/#BASENAME#/$filename_woext/g" $fullfile
done
----

% ./processtxt.sh files

+ directory='files/*.txt'
+ for f in '$directory'
+ echo 'Processing files/testing1.txt...'
Processing files/testing1.txt...
+ fullfile=files/testing1.txt
++ basename files/testing1.txt
+ filename=testing1.txt
+ filename_woext=testing1
+ sed -i '' s/#BASENAME#/testing1/g files/testing1.txt

[fletcher]
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/bbedit/CD71B3FF-B1C9-4BEB-9CE8-82053C11265A%40gmail.com.

Bill Kochman

unread,
Jul 17, 2025, 10:20:27 AMJul 17
to bbe...@googlegroups.com
Hello Fletcher,

I think I have figured out what the problem is. Adding set-x didn’t really help. But, when I tried it this time. it actually worked on 14 files and failed on 717 others. That was when I realized that all 14 files which were successful were one-word file names. All the rest are multi-word file names with spaces between the words.

So, I really don’t want to add dashes to those 717 file names unless I really have to. So do you know if there is a way to edit these scripts so that they will accept spaces between words. I think that will resolve this issue.

Thanks,

Bill Kochman
> To view this discussion visit https://groups.google.com/d/msgid/bbedit/6E08CEFD-AC53-4AC3-8DB2-917C7B0DBC68%40cumuli.com.

flet...@cumuli.com

unread,
Jul 17, 2025, 10:24:44 AMJul 17
to bbe...@googlegroups.com
You can enclose the file name that might have spaces with double quotes. Changing the sed line to this should work.

sed -i '' "s/#BASENAME#/$filename_woext/g" "$fullfile"

[fletcher]
> To view this discussion visit https://groups.google.com/d/msgid/bbedit/B9BC2C54-C72B-4068-A94E-A4E9D4EE7393%40gmail.com.

Bill Kochman

unread,
Jul 17, 2025, 11:31:29 AMJul 17
to bbe...@googlegroups.com
Thanks Fletcher! That worked great! The only remaining problem I had was that it still threw errors because the enclosing folder had multiple words. However, once I changed it to a single word, the issue was resolved, and the script worked as expected.

Kind regards,

Bill Kochman
> To view this discussion visit https://groups.google.com/d/msgid/bbedit/E695F2D8-61D4-4820-8CA6-457773722DF6%40cumuli.com.

Reply all
Reply to author
Forward
0 new messages