Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'unexpected token' in PowerShell when fully pathing executable

Tags:

powershell

Just trying to better understand why the second item below does not work. The first item is simple, the second seems clearer, the third seems unintuitive.

# My path includes pscp so this works.
pscp.exe -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR}

# This does not work. I get unexpected token error. Why? What does that mean?
$PUTTY_PATH\pscp.exe -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR}

# & is required to solve the problem.
& "$PUTTY_PATH\pscp.exe" -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR}
like image 754
Ethan Post Avatar asked Dec 17 '22 02:12

Ethan Post


1 Answers

That's because this is also considered a parse error:

"foo"\pscp.exe 

Whereas this parses correctly as you have found:

"$PUTTY_PATH\pscp.exe"

That resolves to a valid string but as you have already noticed, a string doesn't execute. You have to use the call operator & to invoke the command that is named by the string that follows.

like image 141
Keith Hill Avatar answered Mar 15 '23 04:03

Keith Hill