0

I recently converted a shell script from bash to zsh and got a strange error. I had a command like

HOST="User@1.1.1.1"
scp "$BASE_DIR/path/to/file" $HOST:some\\path

This worked fine in bash, but zsh failed with a bad substitution. I fixed this by change $HOST to ${HOST}, but I'm curious as to why this was necessary. Also, strangely, I had a few such scp commands, and all of them "worked" except the first one. However, I ended up with a file called User@1.1.1.1 on my filesystem which was really unexpected. Why did this subtle change make such a big difference?

Max
  • 15,157
  • 17
  • 82
  • 127
  • 1
    `$HOST = User@1.1.1.1` doesn't work in bash either. See [Command not found in bash variable assignment](https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment) – Charles Duffy Jan 27 '21 at 17:03
  • 1
    What was the actual path? The `:` may have been misconstrued as a word modifier, as `$HOST:s` is equivalent to `${HOST:s}` but not `${HOST}:s`. – chepner Jan 27 '21 at 17:10
  • That was a transcription error on my part. I updated the question to the correct syntax. – Max Jan 27 '21 at 17:10
  • @chepner the path was `$HOST:stage\\something`, so the full path did contain `$HOST:s` – Max Jan 27 '21 at 17:12

2 Answers2

0

Two possible problems (1) Extra '$' at the beginning of the assignment, and (2) embedded spaces.

The first potential problem is the assignment in the style $var=foo. In zsh like in other sh-like engines (ksh, bash, ...), the assignment operation is VAR=value - no $.

The second potential problem are the spaces. No spaces are allowed between the variables name, the '=' and the value. Spaces in the value must be escaped (with quotes, or backslash) Potential correction:

HOST=User@1.1.1.1
scp "$BASE_DIR/path/to/file" $HOST:some\\path
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • This doesn't address the question asked. You can assume the posted assignment isn't what the OP actually runs, since it doesn't work in `bash` as claimed. – chepner Jan 27 '21 at 17:08
0

As chepner mentioned in the commments, zsh has modifiers that are added via :. So $HOST:some was interpreted as $HOST:s by zsh.

A list of modifiers can be found here: https://web.cs.elte.hu/local/texinfo/zsh/zsh_23.html

Max
  • 15,157
  • 17
  • 82
  • 127