Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

shell - zsh substituion - what's the difference between $VAR and ${VAR}?

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?

question from:https://stackoverflow.com/questions/65922075/zsh-substituion-whats-the-difference-between-var-and-var

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...