Learning shell script after years of Windows scripting. I have qBittorrent-nox running in a TrueNAS 12.2 jail. qBittorrent provides a feature to run a command after a torrent download completes. I want to call a shell script to run two chown commands on the folder that is passed as a parameter. The folder will have spaces and may have ampersands, and the shell script fails as a result. The command passed according to qBittorrent's log file is this:
bash /mnt/torrents/Live/fixperms.sh "Name of the folder & description of contents"
This is the code I expected to work:
#!/usr/local/bin/bash
chown -R 1001:1006 $(printf "%q" "$1")
The command is correctly formed, but the script fails as it splits the string. The command it creates works if I echo it and execute it manually. I cannot find a way of making any shell (csh, sh, zsh and bash) NOT split the string at the spaces. I've tried it with single quotes, double quotes and backticks.
I have spent a number of hours on this and I have made no progress. I have tried all four shells, zsh splits the double-quoted string when the documentation says it shouldn't, parameter expansion works in bash but still splits the string. I have no python or perl, and no wish to install them if I can avoid it.
What am I missing?
Bash is 5.0.18, zsh is v5.8, tcsh is 6.2.00