简体   繁体   中英

Sending a pipe as an argument to an external program in Powershell?

I keep getting the following error when running a command to look for the most recently modified directory in my path: remotely from PowerShell:

head: The term 'head' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line 1: char: 95
+ ... d /somedir ; (lastmod=$(ls -tda -- */ | head -n 1); ....

I'm running code something along the lines of the following:

& plink "bob@server" "cd /somedir ; (lastmod=$(ls -tda -- */ | head -n 1); ls -la $lastmod)"

You've encapsulated your commands in double-quotes so the $ signs are being expanded by PowerShell. If you use single quotes this won't happen.

Just to add on to Bruce's answer, you could also escape the special character from your double-quoted string using the grave accent (`), which is the escape character in powershell.

& plink "bob@server" "cd /somedir ; (lastmod=$(ls -tda -- */ `| head -n 1); ls -la $lastmod)"

This means that if you did need any variables inside of your string, you could still utilize special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM