Client Pay Portal
 practical use of bash on Ubuntu on windows

Practical Use of Bash on Ubuntu on Windows

This week marks the one-year anniversary of Windows 10’s release. It seems crazy to me that it’s been a year already, but I honestly can’t imagine going back to Windows 8.1 (blurgh) or Windows 7 (double blurgh). To celebrate this milestone, Microsoft is releasing the Anniversary Update to Windows 10, one of the first major updates, and with it come some long-awaited features to its 300,000,000+ users. Windows ink is a new feature for tablets and touch screens, extensions are coming to the Edge browser (finally), Microsoft Hello is extended for logging into other apps, along with lots of other minor updates, like new a new dark theme and updates to the start menu and notifications. But the feature I’m most excited about is the general availability of Bash on Windows. It’s still technically a beta feature, and you’ll have to enable it in the control panel. You can check out my earlier post for instructions on installation.

So now that you have Bash on Ubuntu on Windows (which can be launched by invoking its name three times in the mirror), what to do with it? Some very smart people have already done some really cool things with Bash in the Windows 10 beta, even getting it to run Linux GUI apps, but for most people it’s really just very handy to have a Linux command line at your disposal without needing to fire up a dedicated Linux machine (which you can do in Azure for just a few bucks!) or switch to a Mac to perform simple tasks. In fact, I’d say what Bash on Ubuntu on Windows is probably most useful for web development and remote server management. Bash is very scriptable, and it’s a great tool for automating web dev tasks, like backing up sites in Apache, etc.

So having said that, in this blog post I want to give you a few commands you should familiarize yourself with to get to know the Bash command line properly. If you’ve ever used the DOS command line or PowerShell, you’ll quickly feel comfortable in Bash. Just remember, command lines are typically not very forgiving of typos, so make sure you’re good with typing in weird-looking cryptic commands. In fact, quite the opposite of PowerShell which is very verbose, Bash prides itself on its terse, short names for commands and essential directories. But much of what you use in Bash has a long and storied history behind it, and has stood the test of time. And just like switching between OSes, you’ll probably experience a little culture shock for the first time in Linux, and things will look strange and pointless. But the more you use it, the more you’ll appreciate it as a powerful tool. Now, let's get to that practical use of Bash on Ubuntu on Windows that I promised you.
 

Commands You Need to Know

Navigation – cd, pwd and ls

Obviously, you need to know how to get around in the Bash shell. And just like DOS, you can use cd to change directories. E.g. cd some_folder will change your working directory to some_folder. If you need to navigate back up a level, you can just use cd .. which will move you to the parent directory. Two special folders you should be aware of are cd / which will take you to the root directory and cd ~ which will take you to your home folder. Linux doesn’t have drive letters like Windows, so the root directory is the top level of the file system. Another command that comes in very handily is pwd, which will print the working directory to the screen, i.e. show you which folder you’re currently in. And of course, you need to see what’s in the current working directory. For that, you’ll use the ls command (that’s a lowercase L, not an uppercase i). Navigate to a directory and type ls -a to show all the files in that directory. Take a minute to move around the filesystem and get used to not using a mouse to switch between folders. And what was up with that -a after the ls command? We can look that up to see what that does!
 

Getting help – man

Learning to use the man pages (manual) is the best thing you can do to teach yourself the Bash environment. Most Linux commands (maybe all?) have different flags you can set, and parameters you can pass to it. The way to discover these flags and their proper usage is to look them up in the reference manual. Any command you’re unfamiliar with you can simply type man and then the command. E.g. man pwd will give you a brief synopsis of what the pwd command does, and will show you some different flags you can set. If you now run man ls you’ll get a significantly longer list of options that you can use. Just remember, man is your friend!
 

Installing software – apt-get and curl

In command line Linux there’s really no equivalent of an App Store, but that doesn’t mean you can’t install new software. In fact, one of the first things you should do on any Linux machine is to run updates for any installed software. First, you refresh the list of repositories to pull from by running the command sudo apt-get update. This will take a few seconds to complete, and the next step you should take is sudo apt-get upgrade which brings all your software up to the latest version. Once that is done, you can search for new command line apps to download. apt-cache search app_name will search the repository for whatever it is you’re looking for (like an FTP server, for instance). Once you’ve found what you want you can run sudo apt-get install app_name to download and compile that specific app. Since all of these commands are prepended with sudo, you’ll be running these as root, and you will be required to re-enter your password as a safety precaution. Another command line utility you should get familiar with is curl which is short for “see URL”. It’s great for downloading something from a specific address, and something I use often. Check the man page to see how to use it.
 

Connecting to other Linux machines – ssh and scp

If you want to get files from a server, curl is great. But what if you want to connect to another Linux machine? That’s where ssh comes in. It stands for secure shell, and it simply establishes a secure connection between you and the remote computer, and then the Bash commands you enter will be executed on the remote machine. You’ll need to have the username, password and IP address or URL of the remote computer, and make sure ssh is enabled, but it’s very simple to connect. Simply punch in ssh username@exampledomain.com and you will be prompted for the password. The first time you connect you’ll have to verify that you want to connect, but that’s it… you’re in! If you don’t necessarily want to gain shell access to a remote computer, and just want to copy a file to it, then scp is your friend. It securely copies a file from your machine using the same protocol that ssh uses. The syntax is pretty simple: scp localfile username@exampledomain.com:remotedirectory/remotefile
 

Editing files – nano and vi

In Bash pretty much everything is configured through plain text files. There’s no registry to mess with, or settings within apps. Just text files. Want to configure the boot sequence? Edit a text file. Want to change how your webserver handles incoming requests? Edit a text file. So having a good text editor is crucial in Bash. Fortunately, there are many options. The most popular, especially for beginners in Linux, is nano. It’s simple to use, just type nano filename to edit that file. The instructions are pretty self-explanatory once you’re in nano, but in case you’re confused the ^ symbol is the control key on your keyboard. So control + x will exit out of nano. More advanced users will want to use vi to edit text files, because it’s an incredibly powerful text editor. However the learning curve is steep. But you’ll know you’ve arrived as a bash power user when you start using vi for everything!

Obviously, there are so many things to learn in Bash, and there are lots of good resources out there to teach you. But I wanted to write this blog post to get fellow Windows enthusiasts excited about the new Linux subsystem in Windows 10, and to give you a launching pad to start exploring!
 
Check out some of my other Microsoft Loves Linux Posts
Bash on Ubuntu on Windows
Bash 101 for the Windows User

Author

Samuel O. Blowes, Director of Solution Consulting
Samuel O. Blowes

Director of Solution Consulting