{"id":365,"date":"2016-10-19T21:54:02","date_gmt":"2016-10-20T05:54:02","guid":{"rendered":"http:\/\/blogs.msdn.microsoft.com\/commandline\/?p=365"},"modified":"2019-02-25T22:49:27","modified_gmt":"2019-02-26T06:49:27","slug":"interop-between-windows-and-bash","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/commandline\/interop-between-windows-and-bash\/","title":{"rendered":"Interop between Windows and Bash"},"content":{"rendered":"<blockquote>\n<p><strong><span style=\"color: #ff6600\">Note: Also be sure to read <span style=\"color: #0000ff\"><a href=\"https:\/\/blogs.msdn.microsoft.com\/commandline\/2016\/11\/10\/more-easily-invoke-windows-apps-and-tools-from-bash-on-windows\/\">the follow-up to this post<\/a>\u00a0<\/span>that covers a subsequent\u00a0improvement in that you no longer have to specify the absolute path to Windows executables if they exist on your path! <\/span><\/strong><span style=\"color: #ff6600\">Content below updated to reflect this change!<\/span>\n  Windows 10 Insider build #14951 has just landed and includes a very exciting new feature that we know you&#8217;re going to love: <strong>Bash <--> Windows Interop<\/strong>!<\/p>\n<\/blockquote>\n<p>Many of you have been <a href=\"https:\/\/wpdev.uservoice.com\/forums\/266908-command-prompt-console-bash-on-ubuntu-on-windo\/suggestions\/13284702-let-us-launch-windows-processes-from-bash\">asking for this capability<\/a> for quite a while, and now it&#8217;s finally here! Starting with build #14951, you can:<\/p>\n<ul>\n<li>Call Windows executables from within Bash<\/li>\n<li>Invoke Linux binaries and capture output from Windows<\/li>\n<\/ul>\n<p>Let&#8217;s take a closer look at these scenarios:<\/p>\n<h2>Call Windows executables from within Bash<\/h2>\n<p>From within a Bash\/WSL console can invoke Windows executables by specifying\u00a0the (correctly-cased) name of the executable, including its <code>.exe<\/code> extension:<\/p>\n<pre>$ notepad.exe\u00a0[filename]<\/pre>\n<p>This will launch the Notepad text editor (opening the requested file if specified):<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/notepad.png\"><img decoding=\"async\" width=\"600\" height=\"383\" class=\"alignnone size-medium wp-image-755\" alt=\"notepad\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/notepad-600x383.png\" \/><\/a><\/p>\n<p>Notice that in the example above, we didn&#8217;t have to specify the full path to notepad.exe: That is because\u00a0<strong>when Bash is launched, the Windows path is appended to the Linux path<\/strong>, so you can invoke any\u00a0.exe that exists on your Windows path by simply typing its\u00a0(correctly-cased) name\u00a0and .exe extension!<\/p>\n<p>If\u00a0you want to invoke an\u00a0.exe that is not on your path, specify the full path to the exe itself:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/3-Path.png\"><img decoding=\"async\" width=\"600\" height=\"90\" class=\"alignnone size-medium wp-image-435\" alt=\"Adding Windows Folders to the Bash Path\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/3-Path-600x90.png\" \/><\/a><\/p>\n<p>You can also create aliases to commonly used tools if you wish:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/3-Alias.png\"><img decoding=\"async\" width=\"600\" height=\"77\" class=\"alignnone size-medium wp-image-375\" alt=\"3-Alias\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/3-Alias-600x77.png\" \/><\/a><\/p>\n<p>So what can you do with this new-found capability to invoke Windows executables from within Bash? Common scenarios include:<\/p>\n<ul>\n<li>Invoke build tools like MSBuild to automate your build\/CI system\/processes (see above)<\/li>\n<li>Launch Windows tools &amp; IDE&#8217;s like Notepad, Visual Studio \/ VSCode \/ Sublime \/ Notepad++ \/ etc. to edit files &amp; projects <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/1-NotepadFromBash.png\"><img decoding=\"async\" width=\"600\" height=\"396\" class=\"alignnone size-medium wp-image-396\" alt=\"Open Notepad from Bash\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/1-NotepadFromBash-600x396.png\" \/><\/a><\/li>\n<li>Execute .bat\/.cmd scripts by calling <code>cmd.exe \/C ...<\/code>\u00a0:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/4-DirFromBash.png\"> <\/a><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/4-DirFromBash.png\"><img decoding=\"async\" width=\"600\" height=\"393\" class=\"alignnone size-medium wp-image-395\" alt=\"Cmd 'dir' from Bash\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/4-DirFromBash-600x393.png\" \/><\/a><\/li>\n<\/ul>\n<blockquote>\n<p>Note: If you try to launch a Windows tool, asking it to open a file that is located in your Linux filesystem, it will be\u00a0unable to open the file (it will appear to be &#8220;missing&#8221;) and Bash will inform you of this problem stating that it was : &#8220;Unable to translate current working directory. Using C:\\WINDOWS\\system32&#8221;. <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/NoFileNotepad.png\"><img decoding=\"async\" width=\"600\" height=\"404\" class=\"alignnone size-medium wp-image-775\" alt=\"nofilenotepad\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/NoFileNotepad-600x404.png\" \/><\/a> This is due to a current limitation in WSL wherein <a href=\"https:\/\/blogs.msdn.microsoft.com\/commandline\/2016\/11\/17\/do-not-change-linux-files-using-windows-apps-and-tools\/\">Windows apps should NOT be used to\u00a0open files in the Linux\u00a0filesystem<\/a>. Opening files in the Windows filesystem is fully supported using both Windows apps and Bash\/Linux tools (via \/mnt\/<drive>\/&#8230;), but\u00a0<strong>avoid opening Linux files using Windows apps <span style=\"text-decoration: underline\">at all\u00a0cost!<\/span><\/strong><\/p>\n<\/blockquote>\n<h2>Capturing Bash output from Windows<\/h2>\n<p>As useful as it is to invoke Windows executables from within Bash, it&#8217;s also incredibly useful to invoke Linux applications and tools from Windows, and capture and process their output in Windows apps.<\/p>\n<p>In the example below, we&#8217;ll call <code>bash.exe -c ...<\/code> to return a list the default Linux user&#8217;s root folder and see how many sub folders contain the string &#8220;sr&#8221; using PowerShell&#8217;s <code>select-string<\/code> cmdlet:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/6-lsFromPowerShell1.png\"><img decoding=\"async\" width=\"600\" height=\"189\" class=\"alignnone size-medium wp-image-425\" alt=\"Invoking Bash from PowerShell\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/33\/2019\/02\/6-lsFromPowerShell1-600x189.png\" \/><\/a><\/p>\n<p>To learn more about how Bash <--> Windows interop works, be sure to <a href=\"https:\/\/blogs.msdn.microsoft.com\/wsl\/2016\/10\/19\/windows-and-ubuntu-interoperability\/\">read and\/or watch the\u00a0accompanying blog and video<\/a> where Ben Hillis\u00a0takes Seth Juarez on a tour of this amazing new feature&#8217;s internal workings!<\/p>\n<p>Stay tuned for more Windows 10 command-line coolness in the coming weeks \ud83d\ude09<\/p>\n<p>Rich, on behalf of the Bash\/WSL and Console teams.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: Also be sure to read the follow-up to this post\u00a0that covers a subsequent\u00a0improvement in that you no longer have to specify the absolute path to Windows executables if they exist on your path! Content below updated to reflect this change! Windows 10 Insider build #14951 has just landed and includes a very exciting new [&hellip;]<\/p>\n","protected":false},"author":910,"featured_media":4180,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-on-ubuntu-on-windows"],"acf":[],"blog_post_summary":"<p>Note: Also be sure to read the follow-up to this post\u00a0that covers a subsequent\u00a0improvement in that you no longer have to specify the absolute path to Windows executables if they exist on your path! Content below updated to reflect this change! Windows 10 Insider build #14951 has just landed and includes a very exciting new [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/users\/910"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media\/4180"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}