microsoft loves linux bash 101

Bash 101 for the Windows User

Microsoft Loves Linux!

A Crash Course

In my previous article I showed you how to install bash on your Windows 10 PC. This is a godsend for many web developers and sysadmins who have been forced to use 3rd party tools on Windows to administer Linux servers remotely. But for many long-time Windows users and IT professionals, the Linux command line can be confusing and disorienting. Some things in the bash terminal require a slightly different way of thinking.

I’ve been using Unix and Linux operating systems in some form or another since the 90’s, but I also work as a Windows administrator, so in this article I’ll attempt to help you grok the Linux system and show you some of the ways it differs from what you’re used to in Windows.

I would like to briefly point out that Bash on Ubuntu on Windows is part of the Open Source Software movement, and that a large part of what makes Linux Linux is the general concept of sharing and giving back and contributing. The bash shell on Windows is Beta, and it needs your voice. This isn’t the time to be a consumer, this is the time to be a contributor! If you have feedback, you should submit it here: https://github.com/Microsoft/BashOnWindows/issues

 

Fundamental Differences in Thinking

Don’t run with administrative privileges (or scissors)

Okay, so we all know you shouldn’t run as Administrator in your main account. But it is tempting, because it makes life so much easier. However, in Linux it is very poor practice to run as administrator… or root in the proper parlance.  You should never run with elevated privileges in bash, instead you should run individual commands that require administrative privileges… individually. The correct way to run these commands is to prepend them with sudo (super user do). Having said that… the current implementation of bash in windows doesn’t follow the traditional user account model. In an upcoming release of bash you will have to set up an account when you first install, and then you will have to use sudo to run commands as root. For now, however, you have two choices; when you launch the bash app you will not have root privileges (even though it says “root” at the prompt). Alternately, you can launch the bash app as administrator and then everything you do in the terminal will run as root. Just be careful… this is dangerous!
 

Drive letters vs. Mount points

Possibly one of the most confusing things for people using a *nix based file system for the first time is the lack of drive letters (alternately, for those of us coming to Windows from Linux, we’re confused by the need for them!) In Windows, each drive is assigned its own letter. In fact, even network shares can be assigned their own drive letters. In bash, all file paths start at the top level with a simple forward slash: /. All your local drives are mounted at /mnt. So if you want to get to your C:\ drive just mosey on over to /mnt/c/.
 

Tab auto-completion

If you’ve used PowerShell you are already familiar with tab auto-completion. It’s greatly simplifies entering commands, and means you don’t have to memorize everything. It also drastically reduces the time it takes to enter a long file path. Tab completion works a little differently than PowerShell, however. When you start typing and hit [Tab] the shell will attempt to finish the command or path for you. If it has multiple options, though, it will pause and wait for you to type more. If you hit [Tab] again the shell will present you with all the options available. Just keep on typing and tabbing until you finish the command!
 

Installing “apps”

The equivalent of the Windows App Store in Linux is the Advanced Package Tool. To install apps and utilities from the command line, use the apt-get install command. This will check the online debian repository for the utility, download the app, and download any additional related apps that are needed, then install everything. After that, you can run the app by simply typing in its name at the command line and hitting [Enter]. E.g. apt-get install cmatrix. Then run cmatrix. (Hit [Q] to exit).

command window for bash on ubuntu on windows

 

Configuration files

In the wonderful world of Windows, most apps store their configurations and preferences in the Windows Registry. By contrast, most apps running in bash store their configuration files in flat text files, often ending in .conf. This makes managing apps and daemons (services) relatively simple once you get the hang of using a command line text editor (more on that later!). A good example of this is the Apache Web Server running in Linux. The daemon is called Apache2, and the configuration file for all the settings is located at /etc/apache2/apache2.conf which is editable in any text editor. Very handy!
 

Piping commands

Another striking difference between Windows and Linux is that in Windows, often apps are monolithic, and perform many different functions. Microsoft Word, for example, allows you enter and edit text, format it, print it, merge it with a database, spellcheck the text, embed an image, etc. In bash there are lots of built in apps and utilities, and hundreds more in the apt-get repository, but generally each app only does one thing. The power of the command line comes in when you string those simple commands together. To string the commands together, you just “pipe” the output of one command into the input of the next. And you do this using the pipe key: [|]. For instance, ls lists all the files in the current folder (much like Dir in DOS). wc, on the other hand counts words and lines. So to see how many files are in the current directory you could run a command like this: ls –l | wc –l, which would list all the files and pipe them in to the word count app.
number of files in directory command

No feedback = good news!

One last thing that may be confusing to first time Linux users: When you run a command, often there is no feedback on the screen. In bash this is a good thing. No news is good news. Typically, when something goes wrong you get a screen full of error messages, so if you run a command and nothing happens, that probably means everything went well.
 

Some Basic Commands

I want to wrap up this article with some basic commands to get you started in bash. There are literally thousands of bash utilities available, but you will need to get around the file system.

pwd – This shows you your current location (e.g. Your desktop) in the file system. Handy if you get lost!

cd – This one is the same as in DOS, and means “change directory”. You can cd into a folder, or if you wish to go up a level you can type cd .. You can also jump straight to your home folder by just typing cd

ls – This command lists everything in the specified directory. If no directory is specified, it will list the files in the current directory. I like to use it with the following flags, for more detail: ls –Gal which shows ALL files (even hidden ones), outputs them in a long list format, and color codes them based on their permissions and file type.

ps – This will show you all running processes. This command is very handy when piped into a grep command, which searches for a specific word. A good example of this is if I want to see if Apache is running I can simply run: ps | grep “Apache”

bash on ubuntu on windows command window
And last, but not least, you need to learn a command line text editor. Vim (often referred to as just Vi) is probably the most popular, but has a pretty steep learning curve and seems very unintuitive. Nano may be a better option for beginners.

In a future article I’ll give you some practical usage tips on using Bash on Ubuntu on Windows for real-world administration tasks.
Comments
 
comments powered by Disqus

Author

Samuel O. Blowes, Director of IT
Samuel O. Blowes

Director of IT

Read more