Troubleshooting a simple shell script with KM variables in Macro

I'm having a little trouble with a shell script and I was hoping someone might have some suggestions to either troubleshoot it or see if I missed something.

The Macro is designed to export a note out of Bear as HTML using an Applescript using UI scripting (unfortunately), then I pass the resulting HTML file to webarchiver using a shell script to be converted to a web archive file (for embedding image) before importing it into DEVONthink and trashing the HTML file and attachments (images) directory.

I could pass the shell command through AppleScript but I would like to add a few extra things and I'm a bit more comfortable with Bash than I am with AppleScript. Depending on the problem, that's certainly still an option.

Here's what I have so far:

The AppleScript works properly, it hands off the filename to KM as the variable Local__FileName. I then filter the extension off and save it as Local__FileNameWithoutExtension.

Since the .html file isn't written instantaneously I couldn't use Pause Until... ā†’ A file exists so for now I'm just adding a 10 second delay.

Then I run this simple shell script:

cd "$KMVAR_SystemPaths__ActionDirectory"

/usr/local/bin/webarchiver -url "$KMVAR_Local__FileName" -output "$KMVAR_Local__FileNameWithoutExtension.webarchive"

sleep 5

rm -rd "$KMVAR_Local__FileNameWithoutExtension"
rm "$KMVAR_Local__FileName"

Unfortunately, this always fails to generate the web archive file, when I include the rm commands it will correctly delete the folder so I know it's getting through the script.

I also don't get any errors or output.

I'm pretty sure the command should work correctly and for some reason the variables are being passed to the shell script incorrectly because I recreated the command as a Set System Clipboard to Text and if I then paste that into the second execute shell script item and run that the script runs correctly, the web archive is generated and both the HTML file and the folder are deleted.

Here's the example that worked fine in the second shell script:

cd "/Users/cwhite/Desktop/Action/"

/usr/local/bin/webarchiver -url "Advanced Data Management for iOS with DEVONthink.html" -output "Advanced Data Management for iOS with DEVONthink.webarchive"

sleep 5

rm -rd "Advanced Data Management for iOS with DEVONthink"
rm "Advanced Data Management for iOS with DEVONthink.html"

Any idea what's going on?

For reference, here's the full AppleScript:

tell application "Bear"
end tell

tell application "System Events"
	tell process "Bear"
		click menu item "Export Notes..." of menu "File" of menu bar 1
		# Set export settings
		tell sheet 1 of window 1
			# Grab the filename as a variable to pass to Keyboard Maestro
			set filename to value of text field 1
			# Set path
			keystroke "G" using {command down, shift down}
			delay 1
			tell sheet 1
				# TODO: Don't hard-code this, get the path from KM var (SystemPaths__ActionDirectory)
				set value of combo box 1 to "~/Desktop/Action"
				tell button 2
				end tell
			end tell
			# Set tag 
			set value of text field 2 to "! km:bear ā†’ devonthing: web archive"
			# Set format to HTML
			tell pop up button 2
				tell menu 1
					click menu item "Html"
				end tell
			end tell
			# If there are attachments in the note ensure they are exported
			set exportCheckbox to checkbox "Export attachments"
			tell exportCheckbox
				set checkboxStatus to value of exportCheckbox as boolean
				if checkboxStatus is false then click exportCheckbox
			end tell
			# Export
			tell button 2
			end tell
		end tell
	end tell
end tell

# TODO: Find a way to delay until file isn't locked or Bear isn't busy rather than the 10 second delay.

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	setvariable "Local__Filename" instance kmInst to filename
end tell