To capture a window using screencapture in Terminal, you have two options:
Interactively
-i capture screen interactively, by selection or window
control key - causes screen shot to go to clipboard
space key - toggle between mouse selection and
window selection modes
escape key - cancels interactive screen shot
You can use the -o option in conjunction with the -i option to not capture the shadow of the window, e.g.: -oi
Programmatically
-l<windowid> capture this windowsid
The -i option appears to be self-explanatory while the -l<windowid> option gives no additional info as with the -i option.
The <windowid>, for some windows can be ascertained by using AppleScript in the following example:
screencapture -l$(osascript -e 'tell app "Safari" to id of window 1') test.png
You can use the -o option in conjunction with the -l option to not capture the shadow of the window, e.g.: -ol
Some applications will not work with this method, however here are some alternatives:
Reference: How do I find the windowid to pass to screencapture -l?
Note: I am not affiliated with these projects.
-R<x,y,w,h> capture screen rectoption not a "specific window" as in the title of this question, which will require the use of either the-ior-l<windowid>options depending on specifically how it is to be captured. I guess one could say it's somewhat related, but it's not a duplicate. – user3439894 Dec 24 '19 at 17:08