3 ms·
Function calls don't need parens because it's an interactive shell, and nobody writes Bash or Command prompt lines like: user@server$ grep('word', 'file.tx
by jodrellblank 3y ago
Function calls don't need parens because it's an interactive shell, and nobody writes Bash or Command prompt lines like:
user@server$ grep('word', 'file.txt')
C:\ > notepad.exe()
And if they did make that the requirement it would be an instant dealbreaker for a lot of people coming from other shells. It is indeed a similar story with quotation marks around strings, in that you don't want to have to write:
user@server$ grep 'word' 'file.txt'
when you could write
user@server$ grep word file.txt
If the parser can tell from the context that the next token should be a string, it will read it as a string. If it can't tell, and the next token might be a command but you want it to be a string, then you have to quote it. If there's a space e.g. in the file path and you want it to be read as a single token, you have to quote it. Always quoting won't break anything, it's just unnecessary.
- benbristow 3y agoI've often gotten caught out using commas to separate arguments like most other scripting languages ever. One of those things you need to remember when writing Powershell scripts!
- jodrellblank 3y agoCan you give an example of where/how? I imagine you don't get caught out writing Bash scripts like: grep -e,-i,'\bneedle\b',file.txt | cut -F,2,-d,':' Because that's so contextually distinct from Python/JavaScript/Ruby; in shells (and APL, J, LISPs), arrays of commands and their arguments are space separated rather than comma separated. Similar in PowerShell, where it looks like shell it tends to work like shell: get-childitem -Path c:\ -Filter *.txt -Force Where it looks like scripting language it tends to work like one: $host.ui.WriteLine('Green', 'Yellow', 'Hi')
- benbristow 3y agoWith functions function Get-Something { Param ( [Parameter(Mandatory=$true, Position=0)] [string] $Name, [Parameter(Mandatory=$true, Position=1)] [int] $Id ) # Write the output to the console Write-Output "Name: $Name, Id: $Id" } Get-Something "Alice" 42 If you were to call Get-Something("Alice", 42) It would pass "Alice", 42 to the first parameter. You can use named parameters to get round this, e.g. Get-Something -Name "Alice" -Id 42 Still a bit of a gotcha!