{"id":4198,"date":"2017-09-21T21:16:34","date_gmt":"2017-09-22T02:16:34","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=4198"},"modified":"2017-09-21T21:22:25","modified_gmt":"2017-09-22T02:22:25","slug":"how-to-import-any-linux-command-into-your-windows-10-environment","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/how-to-import-any-linux-command-into-your-windows-10-environment\/","title":{"rendered":"How to import any Linux command into your Windows 10 environment"},"content":{"rendered":"<p>At NeoSmart Technologies, we&#8217;re\u00a0<em>huge<\/em> fans of the new Windows Subsystem for Linux,<sup id=\"rf1-4198\"><a href=\"#fn1-4198\" title=\"Yes, we refuse to call it by its ungodly &ldquo;Bash on Ubuntu on Windows&rdquo; moniker\" rel=\"footnote\">1<\/a><\/sup> and have spent a lot of time trying to make the transition between the native Win32 subsystem and the Linux\/WSL subsystem as seamless as possible.<\/p>\n<p>For those of you that haven&#8217;t already seen it, we recommend reading our previous article\u00a0<a href=\"https:\/\/neosmart.net\/blog\/2017\/meet-your-new-best-friend-for-wsl\/\" rel=\"follow\">Meet $, your new best friend for WSL<\/a>\u00a0for an introduction to WSL and <code>$<\/code>, our nifty helper utility that lets you directly run Linux commands in your Windows workflow. In brief, we developed <code>$<\/code>\u00a0(also known &#8211; though less affectionately &#8211; as <code>RunInBash<\/code>) to make it possible to run Linux utilities directly from within a Windows workflow, complete with arguments, <code>stdin<\/code>,\u00a0<code>stdout<\/code>, and\u00a0<code>stderr<\/code>\u00a0redirection, and more.<\/p>\n<p><!--more--><\/p>\n<p>While <code>$<\/code>\u00a0makes it dead simple to run any Linux command by prefixing it with &#8212; yup, you guessed it &#8212; <code>$<\/code>, sometimes even that isn&#8217;t easy enough. For example, a lot of the developers at NeoSmart don&#8217;t use bash as their primary shell;<sup id=\"rf2-4198\"><a href=\"#fn2-4198\" title=\"And, honestly, who can blame them? Have you seen the syntax bash uses to access and escape elements of an array?\" rel=\"footnote\">2<\/a><\/sup> so having to run another shell, say <a href=\"https:\/\/github.com\/fish-shell\/fish-shell\" rel=\"nofollow\">fish<\/a>, by keying in <kbd><span class='kbdkey'><img src='https:\/\/neosmart.net\/wiki\/wp-content\/uploads\/sites\/5\/2015\/02\/WinKey.svg' class='winkey colorbox-4198' \/><\/span><\/kbd>+<kbd><span class='kbdkey'>R<\/span><\/kbd> then having to type in <code>$ fish<\/code>, while still a distinct improvement over <code>bash -c fish<\/code>, just didn&#8217;t cut it. And it&#8217;s not just a shell &#8211; there are a lot of &#8220;stand alone&#8221; command line utilities that developers rely on enough to launch directly without first entering a shell, such as <code>ssh<\/code>\u00a0or <code>nvim<\/code>.<\/p>\n<p>The ultimate solution we&#8217;ve landed on is a simple <del>bash<\/del>\u00a0batch script that you can copy locally as many times as you like to launch any Linux tools you desire &#8211; <em>without ever writing or changing a line of code<\/em>.<\/p>\n<p>Let&#8217;s jump straight into the batch file and then we&#8217;ll talk about how it works:<\/p>\n<pre><code class=\"language-batch\">@echo off\r\n\r\nset cmd=%~n0\r\n$ %cmd% %*\r\n<\/code><\/pre>\n<p>Disappointed? Why, were you expecting more? That&#8217;s <em>really<\/em> all there is to it. The batch file above, when copied to a file called, let&#8217;s say, <code>ssh.bat<\/code>\u00a0will run <code>ssh<\/code>\u00a0under WSL, forwarding any arguments you pass to it along. All you have to do is put it in your <code>PATH<\/code> somewhere!<\/p>\n<p>So how does it work? First, the batch file gets its own name via the <code>%~n0<\/code>\u00a0magic syntax.<sup id=\"rf3-4198\"><a href=\"#fn3-4198\" title=\"Yes, batch is ugly&hellip; perhaps even uglier than bash.\" rel=\"footnote\">3<\/a><\/sup> This gives us the file name without the directory component, so in this case, <code>ssh<\/code> &#8211; which will be taken as the name of the command to run on the Linux side of things.<\/p>\n<p>The rest of the magic is all thanks to <code>$<\/code>, which takes care of escaping arguments and passing them to <code>bash.exe<\/code>\u00a0in such a way that you can keep your sanity instead of pulling out your hair trying to figure out how to double-escape an <code>'<\/code>\u00a0in an argument enclosed in <code>\"<\/code>\u00a0when your command contains spaces and you need to pass it in to <code>bash -c<\/code>\u00a0enclosed in another&#8230; forget it. That&#8217;s what <code>$<\/code>\u00a0is there for.<\/p>\n<p>Have another Linux tool you use often? Just copy your <code>ssh.bat<\/code>\u00a0over to <code>tool.bat<\/code>\u00a0and you&#8217;ll be on your merry way. If you want to get all fancy, you can even create a symlink instead, and then if you ever want to make some changes to this batch file (though hopefully you&#8217;ll never need to) you&#8217;ll only need to change one and the rest will automatically follow suit. It really couldn&#8217;t get any easier than this.<\/p>\n<p>If you don&#8217;t already have <code>$<\/code>\u00a0installed, you can either <a href=\"https:\/\/neosmart.net\/RunInBash\/\" rel=\"follow\">download it here<\/a>\u00a0or install it with chocolatey via <code>choco install RunInBash<\/code>.<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-4198\"><p>Yes, we refuse to call it by its ungodly &#8220;Bash on Ubuntu on Windows&#8221; moniker&nbsp;<a href=\"#rf1-4198\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><li id=\"fn2-4198\"><p>And, honestly, who can blame them? Have you seen the syntax bash uses to access and escape elements of an array?&nbsp;<a href=\"#rf2-4198\" class=\"backlink\" title=\"Jump back to footnote 2 in the text.\">&#8617;<\/a><\/p><\/li><li id=\"fn3-4198\"><p>Yes, batch is ugly&#8230; perhaps even uglier than bash.&nbsp;<a href=\"#rf3-4198\" class=\"backlink\" title=\"Jump back to footnote 3 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>At NeoSmart Technologies, we&#8217;re\u00a0huge fans of the new Windows Subsystem for Linux,1 and have spent a lot of time trying to make the transition between the native Win32 subsystem and the Linux\/WSL subsystem as seamless as possible. For those of &hellip; <a href=\"https:\/\/neosmart.net\/blog\/how-to-import-any-linux-command-into-your-windows-10-environment\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[568,5,965,887,950],"class_list":["post-4198","post","type-post","status-publish","format-standard","hentry","category-software","tag-bash","tag-linux","tag-runinbash","tag-windows-10","tag-wsl"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-15I","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4198"}],"version-history":[{"count":14,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4198\/revisions"}],"predecessor-version":[{"id":4212,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4198\/revisions\/4212"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}