How to quote a path variable "$1" that contains spaces?

I am calling a script from a script and passing a positional argument {folder} — whose value is a path that contains spaces so it needs to be quoted.

The second script is:

#!/bin/bash -xu
find "$1" -type f -iname "*.srt" | grep -v -e "\\.eng" -e "].srt" -e "].[0-9].srt" | while read -r file; do trash -F "$file";done;

I have been unsuccessful in adding quotes around the path variable $1. (I am guessing this is the issue, I would like to log all of the output to pinpoint it but not sure how to do that either)

I tried escaping it but that did not work \""$1"\"

The first script:

#!/bin/bash
filebot -exec /Users/john/.filebot/scripts/delete_non-english_subtitles.sh {folder}

The second script:

find "$1" -type f -iname "*.srt" | grep -v -e "\\.eng" -e "].srt" -e "].[0-9].srt" | while read -r file; do trash -F "$file";done; \
echo "$1" >> ~/Desktop/SsubDeletetest.txt; \
echo "$1" | awk -F"/" '{ print $NF }' >> ~/Desktop/subtitletest.txt; \

I added the echo lines to get an idea of how the argument value was formatted. Those two echo lines are working. There is output in those txt files. But the Find command never runs when called. I can run the same Find commands in terminal when I quote the path but argument apparently doesn't get quoted because at the first space in the path value the path is clipped.

I should add that the first script is being called by an application, not in Terminal. So the only way to see the results is to output to a text file... I think.

  1. Your post-process script is just deleting *.srt files, so that'd be a rather straight-forward -exec use case. You'll want to refactor that into it's own standalone script and then have filebot call it via -exec on the destination folder as explained above.

Here's what the call stack would look like:

1. qBT calls your qBT_e-program.sh
1.1. qBT_e-program.sh calls filebot
1.1.1. filebot -exec calls your delete-srt-files.sh passing along the destination {folder} as Argument $1
1.1.1.1. delete-srt-files.sh calls find "$1" ...```

Adding more to help troubleshoot. Still stuck on this. I learned how to get some output and have some clues but can't figure the issue out.

Again, When I add the path into the script instead of using the quoted variable "$1", the script works. When I use the variable it fails and results in this error output. This is being called from the initial script with quoted argument "{folder}". I have tried variations on treating the variable: "{$1}" \""$1"\" "$1/" -$1. I thought maybe the filebot -exec command was already quoting the variable and my quotes were actually unquoting it.

+ read -r file
find: /Volumes/PlexMedia/PlexServer_1/Movies/Paper: No such file or directory
find: Lives: No such file or directory
find: (2021)/Paper: No such file or directory
find: Lives: No such file or directory
find: (2021): No such file or directory
find: {imdb-tt13045890}: No such file or directory
find: [[en]: No such file or directory
find: 2.40∶1: No such file or directory
find: WS: No such file or directory
find: 1080p: No such file or directory
find: WEB-DL: No such file or directory
find: x265: No such file or directory
find: 584ECE9D].mp4: No such file or directory
echo result /Volumes/PlexMedia/PlexServer_1/Movies/Paper Lives (2021)/Paper Lives (2021) {imdb-tt13045890} [[en] 2.40∶1 WS 1080p WEB-DL x265 584ECE9D].mp4

I can see the variable is being split at the spaces. But it's quoted! This is a different file but same script calling it form Terminal:

Sat Mar 13 12:12:30
iMac191:Klara (2010) john$ /Users/john/.filebot/scripts/delete_non-english_subtitles.sh
+ path='/Volumes/PlexMedia/PlexServer_1/Movies/Test Folder (1968)'
+ find '/Volumes/PlexMedia/PlexServer_1/Movies/Test Folder (1968)' -type f -iname '*.srt'
+ grep -v -e '\.eng' -e '].srt' -e ']\.[0-9].srt'
+ read -r file
+ trash -F '/Volumes/PlexMedia/PlexServer_1/Movies/Test Folder (1968)/moviename.swe.srt'
+ read -r file

You've done it correctly in your example.
"$1" will be replaced with "parameter content", quoted.

You can test this by minimising your code further.

#!/bin/bash
find "$1"

This works perfectly:

$ ./find1.sh "foo bar"
foo bar/.DS_Store
foo bar/test
foo bar/test.xcodeproj
foo bar/testUITests
foo bar/testTests

This demonstrates the parameter to the script being correctly provided to the find command.

Your issue must be elsewhere. Make sure you're calling the script with the correct quoting too!

To see how variables are replaced with their contents, add set -x after the shebang.

#!/bin/bash
set -x
find "$1"

Commands run after set -x will be printed prefixed with +, showing the variable substitution:

$ ./find1.sh "foo bar"
+ find 'foo bar'
foo bar/.DS_Store
…

Based on the additional calling script added, it can be seen that the presumption was correct — the script is not being called quoted.

#!/bin/bash
filebot -exec … {folder}

The {folder} is not quoted, so each space separated argument is given as a separate argument.

You need to quote "{folder}" so it's one argument.

echo prints all arguments space separated, so it's not suitable to determine errors in this way.