Skip to main content
9 events
when toggle format what by license comment
Aug 6 at 16:34 comment added ilkkachu (strictly speaking, in what was done in the Q, the resulting string that gets executed on the remote isn't even for ... done file1 file2..., but for ... done<NEWLINE> file1 file2..., which just leads the first filename being used as a command name. So not a very useful result either, but a bit different from what the answer here claims.)
Aug 6 at 16:27 comment added ilkkachu mm, well, let's just say that e.g. ls -l "foo bar" works to list a file with a space in the name, but ssh host ls -l "foo bar" doesn't. And e.g. sh -c 'printf "<%s>\n" "$1"' _ "foo bar" keeps the string intact, but neither ssh host sh -c 'printf "<%s>\n" "$1"' _ "foo bar" or ssh host 'printf "<%s>\n" "$1"' "foo bar" does. (ls and printf as examples only, ofc.) I don't think it's immediately obvious why ssh couldn't act more like sh -c here. I mean, if you know, you know, and it is documented in the man page, but we can't expect people to know everything before asking.
Aug 6 at 15:49 comment added terdon Yes, but they seemed to have expected the array to be passed as an argument to the for loop so they can use $@, and that doesn't make sense irrespective of whether ssh is involved. As I write: "You can't do for file in "$@"; do something; done argument1 argument2, that isn't valid syntax. It has nothing to do with ssh". I am not saying their general issue has nothing to do with ssh, only the specific one I focused on.
Aug 6 at 15:28 comment added ilkkachu um, Isn't that exactly what they're doing? They have ssh "user@host" '[shell script]' "${files_list[@]}" there, and that array expansion "${array[@]}" passes each array element as a separate arg to ssh, right? (Pretty much the same way "$@" works, and what happens in for file in "${array[@]}")
Aug 6 at 15:21 comment added terdon Oh, so you think the OP expected that the list of files would be treated as arguments to ssh somehow?
Aug 6 at 15:17 comment added ilkkachu well, yeah, but it's not like they're just concatenating the shell scriptlet and the filenames themselves; instead they are trying to pass the filenames as separate args, exactly as would be right with any other program. It just doesn't work with SSH in the way one might hope.
Aug 6 at 15:13 comment added terdon Sure, @ilkkachu, $shell -c 'whatever' args would work, but not for ...; done args, that's my point. The positional parameters array has meaning when referring to the arguments given to a shell but not to something like a for loop as the OP was doing.
Aug 6 at 15:09 comment added ilkkachu Well, it kinda has to do with ssh, since the ssh client concatenates all the arguments it gets to a single string instead of passing the command line arguments separately and intact. Doing the similar thing with a shell, like sh -c 'for file in "$@"; do ... done' sh file1 file2 file3... should work.
Aug 6 at 13:10 history answered terdon CC BY-SA 4.0