Have fun with the command line on Windows 10

It is hard to overstate how much has the command line experience on Windows improved in the past few years.

I've always been a Windows guy, but since my early days as a web developer in the year 2000, I had to learn
some Linux to be able to do my job.

Mind you, I love the Linux shell. While not being an expert by any means, I can navigate the system, configure the services,
run deployments, and the like.

On the desktop, however, I've stopped trying to use Linux a long time ago. The only thing that I missed was the bash shell.

Before Windows 10, all I had on the command-line was DOS and Powershell, and I haven't yet got to learning Powershell
enough to be productive. (Time to change that! -ed.)

Fast forward a few years, and we've got the Windows Subsystem for Linux (WSL) and native Linux kernel running inside Windows.

We have multiple distributions that you can download from the Windows Store and use from the command line.

And, there's Docker, which can now also be backed by WSL version 2.

The cherry on the top of this cake is the new Windows Terminal app, also available in the Store. Each shell, once installed, becomes automatically available in the Terminal app. See the screenshot of how my Terminal is equipped as of today:
My Windows terminal with all available shells

(The "DOS but not DOS" is actually Yori, a remake of the classic DOS shell but after sniffing a lot of cocaine - love it! I also learned about it from Scott Hanselman).

There's so much terminal goodness for everyone now, on Windows!

Some of my favorite CLI shortcuts

Here are a few things I do in CLI daily, using any number of shells available to me in Windows liberally.

Fun with curl

I spend a considerable amount of time querying REST endpoints and find the curl command to be my favorite UI. Yes, there's Postman, and SoapUI and whatnot. For a quick call to send or receive some JSON, curl is the fastest tool with the least friction.

You can drop down directly to your WSL distro and run curl there. Or, if you happen to be in a Powershell window (pretty much the default state unless you specify otherwise), preface your curl invocation with wsl and you are good to go. Such as:

wsl curl https://api.mywebsite.com/api/v1/people

If you're piping your output, and the command after the pipe is also a Linux command, you need to preface it with wsl as well. For instance, if I want to pipe my JSON into jq for pretty-printing, the above would read:

wsl curl https://api.mywebsite.com/api/v1/people | wsl jq

The wsl command will send the rest to your default WSL distro, which then does the job. How we can have such seamless interop between Windows and Linux still manages to stun me, in 2020.

Fun with Docker and WSL

Docker on Windows is a no-brainer. Instead of getting a full-blown Windows installer and struggling to configure it correctly, hoping that its Windows support is up-to-par, you simply pull a pre-configured image from Docker Hub, follow the HOWTO to expose the ports, and lo and behold, you can play with your new toy!

I don't necessarily use Docker for my day-to-day infrastructure apps, e.g. Mongo, which I might just as well install and run "natively". For throw-away or experimental play, it's Docker or go away.

WSL helps me in two ways: one obvious, another not so much.

The obvious part is that for servers such as MySQL, which I run inside a Docker container, I don't download a silly Windows GUI tool but install the mysql-client package inside my Ubuntu distro, and talk to my SQL server from the Linux CLI.

mysql -u tomas -h 127.0.0.1 -P 3306 -p

A lot better than downloading, configuring, and dealing with phpMyAdmin or similar.

I could also install the whole server inside my WSL distro, however for the servers I now prefer Docker, and only keep my client apps in WSL. That way I can play with different server versions and not worry much about their configuration, which is a huge win when fooling around with new tech.

The not-so-obvious thing, which is quite new, is that you can now tell Docker to use WSL v2 as its backend.

If I'm reading this correctly, this means that it no longer uses its own Hyper-V virtual machine and instead utilizes a WSL v2 distro that you already have installed. You save some disk space, and the Docker startup is considerably faster.

Fun with ssh and scp

This might be the most obvious, crying-out-loud obvious thing to say, but here it goes anyway: the Windows ssh/scp experience is now on-par with what's available on Linux.

That's because OpenSSH is now included in Windows.

I don't know about you but I've always found the experience of using PuTTY or Cygwin somehow off-putting. Better than nothing but definitely not as fluent as simply typing out the commands in CLI.

I can now drop into the CLI of my VMs within seconds, send files back and forth (well, I don't do that so often but I could!), and integrate these features in my batch files.

Pure joy. I have all the comforts of a Windows desktop but with all the razor-sharp tools of the *nix CLI. Isn't life wonderful?