Unless you’re a diehard Windows user day and night, you’ve probably found yourself in a UNIX shell from time to time.

Maybe you wanted to send a nice and gentle kill -9 to some perky process. Or, you messed up your local Git repository so badly that your IDE detonated with a loud bang, and only the most obscure of Git commands would save the day…

Ah, the shell and the wonders it does. But wait: what shell exactly? Well, the most popular shell is still Bash, which stands for Bourne Again Shell: one of those fun acronyms the *nix world is so fond of. Its first release being already in 1989, it’s still the default shell for many GNU/Linux distributions, meaning: you’re probably using it yourself.

We’ll give you some useful tips to survive life in the shell, and maybe make it even quite comfortable.

Opening a shell

So what if – not an unusual scenario – your company thought it best to equip everyone with a Windows machine? In that case you’ve got a couple of options, luckily.

If you’ve got Git installed, you probably already have a decent shell on your computer: Git Bash. You can just use internal commands such as ls, ps and rm to your heart’s content, but beware: it does not provide a real Linux environment.

Not even running Git Bash in fullscreen will change the fact that you’re actually still running Windows. All is not lost though, read on!

In 2016, Microsoft introduced Windows Subsystem for Linux, WSL in short. As unbelievable as it may sound to anyone knowing just a little bit about Microsoft’s history with respect to Linux, WSL makes it possible for you to install a Linux distribution on your Windows installation!

Have fun at https://docs.microsoft.com/en-us/windows/wsl/install-win10.

Other options include MobaXterm, a free to use tabbed SSH (and SFTP) client, which is also able to open a local terminal, with, again, Bash. This option, again, is just a POSIX layer on top of Windows.

Now you might think, Bash is Bash, so what gives? Well, because you’re still running Windows, you rely on an opiniated view of, for example, where your Windows drives should be mounted. On Git Bash, these will be at /c, /d and so on. In MobaXterm, they’re located at /drives/c, /drives/d (with additional symbolic links in /mnt) and so on.

Something to be aware of if you’re using multiple tools.

A sine wave and a dollar…?

Whether you’re on Windows or Linux, you should be able to find yourself in a shell now. Now, what you’re being greeted with could (in the worst case) look kinda like this:

~ $

Consice, snappy, succinct: whatever you want to call it, we can for sure improve this sucker! The prompt is stored in a variable called PS1. Configuring it, therefore, is as easy as an export PS1=... statement in your .bashrc file.

Now think to yourself: what information could be useful having in your prompt all the time? You’ll have to figure it out yourself. I’ll give you my prompt, with some thoughts that went along designing it. For readability’s sake, I’ve omitted the colors (you can make your own wild choices in that regard).

\t \u@\h:\w$(__git_ps1 " (%s)") $

Let’s walk through this step by step, shall we?

  • \t gives the current time. I find this helpful when scrolling back my terminal screen. When did I run that command again?
  • \u gives the current user. Just so I don’t forget I shouldn’t be running as root all day.
  • \h gives the current hostname (up to the first dot, to be exact). Helpful, but of course much less so when the sysadmins think it’s cool to have servers with names like prd32053, which, naturally, is NOT the production server.
  • \w gives the current directory. Can’t live without this one. Actually, the authors of the original Bourne shell think you can. Try starting sh. Yep, this is as oldschool as it gets!
  • __git_ps1 " (%s)" gives the current Git branch. This is an epic win if you’re using Git. Now, why is this not default in Git Bash?

Again, this is what I like using. Figure it out yourself, for instance by using a website like Bash RC Generator. You won’t find the Git branch snippet there though.

Environmental awareness

Sometimes you might feel lost, and I’m not talking about that hike you had in the mountains the other day. What if you want to know what shell you’re in right now? Try this:

$ ps -p $$
PID   TTY   TIME     CMD
19081 pts/0 00:00:00 bash

$$ gives the PID (process identification number) of the current process, and passing this to ps gives you just the information you need.

A quintessential example of ‘the UNIX way’: commands should be as short as possible. Legend goes, this is because the keys on the original console that ran UNIX were hard to press, causing knuckle pain…

Repeating your arguments

Now that we are on the subject of ‘a string of symbols that has a useful meaning’, how about this one: !$ is a shortcut for the argument of the last command.

So this:

$ ls mail.log
mail.log
$ cat !$

.. does indeed what you think it does. If you’re really into these kind of gems, man bash is a place you’ll love.

To top it all off: !$ is actually a shortcut for the much clearer (you’ll all agree on that) !!:$.

Anybody out there?

Ah, you know how it goes. You’re at work even at 9 o’clock in the evening: you just won’t stop working ’till it’s done. Your colleague promised to be working at home too, but… is he really? Check out if other users are logged in:

$ w
21:06:26 up 2 days, 9:20, 1 user, load average: 0.11, 0.11, 0.09
USER         TTY    FROM           LOGIN@ IDLE JCPU PCPU WHAT
michel       pts/0  192.168.1.57   22:14 1.00s 0.45s 0.01s w

Ha! You knew it! You make a note to confront him tomorrow first thing in the morning. Ah, the prompt and the wonders it does!


That’s all for now. In future articles, we’ll touch on different shells (zsh mainly), and give you some hard-to-find-but-useful features for several standard command line utilities. Thanks for reading!

$ exit
exit: command not found

What the….?!