简体   繁体   中英

What does '@-' mean in PowerShell after pipeline?

In this StackOverflow thread , the suggested answer is supplying magic "stdin" string '@-' to the command option.

# Create data as PS hashtable literal.
$data = @{ user = @{ name = "$Firstname $Lastname"; email = "$adUsername@mydomain.org" } }

# Convert to JSON with ConvertTo-Json and pipe to `curl` via *stdin* (-d '@-')
$data | ConvertTo-Json -Compress | curl.exe mydomain.zendesk.com/api/v2/users.json -d '@-' -H "Content-Type: application/json" -X POST -v -u myuser:mypass

It works like a charm, but what does the magic "stdin" string actually mean?

Thank you!

curl 's -d parameter optionally allows you to supply data via a file (its contents ) rather than by direct value.

So as to distinguish direct values (the default) from a value representing a filename , filenames must be prefixed with @ (which is a common convention in the Unix world).

In the Unix world - is a frequently used convention to represent stdin (standard input) in contexts where filenames are expected (depending on context it may also represent stdout (standard output).

Thus, @- effectively instructs curl to read the -d argument from stdin , which in PowerShell is (always) provided via the pipeline ( | ).

In your PowerShell command @- must be quoted ( '@-' ), because an unquoted @ at the start of an argument is a metacharacter , ie, a character with special syntactic meaning (it serves several functions, including splatting ).
Alternatively, you could escape just the @ itself: `@- (PowerShell's escape character is ` , the so-called backtick).

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