I've built a function in my .bashrc that breaks when it tries to scp files with spaces in their names, but if I run the generated command output from the function on in the shell directly it seems to work fine.
I've tried escaping spaces, and several variations of single and double quotes, the version below is the closest I've gotten to working and I don't understand why it fails.
From .bashrc
push2() {
# parse args, build file list, get suffix from final arg
files=""
i=1
orig_IFS=$IFS; IFS=":"
for arg in $*; do
if [ "$i" = "$#" ]; then
suffix=$arg
else
files="$files $(echo $arg | sed -r 's/ /\\ /')" #escape spaces
fi
i=$(($i+1))
done
IFS=$orig_IFS
# determine prefix and proxy
gen_prefix $suffix
# output generated command for debugging
echo "scp $scp_proxy$files testuser@$prefix$suffix:"
# run command
scp $scp_proxy$files testuser@$prefix$suffix:
}
Running the function still seems to fail even though the output command string appears properly escaped
root@DHCP-137:~$ push2 temp* 42
scp temp temp\ file testuser@10.3.3.42:
temp 100% 6008 1.1MB/s 00:00
temp\: No such file or directory
file: No such file or directory
Running the command it generates works as expected
root@DHCP-137:~$ scp temp temp\ file testuser@10.3.3.42:
temp 100% 6008 896.4KB/s 00:00
temp file 100% 0 0.0KB/s 00:00
root@DHCP-137:~$
Additional Info: GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) - running on Debian 9