Q&A: Shell Trickery

Here’s a few questions from people regarding the shell that are just simple misconceptions about what it does or questions about small little concepts.

Finder Tracking Bash

My question is in regards to the Appendix Exercises. So I am operating on a MacBook Air, and for my Terminal, I am typing out my commands. I also have open my directories folder so when I am doing commands, I can see the effect of it. What confuses me is the cd ~ command. Let’s say I click on my documents and it takes me to a subfolder. I then go back to the Terminal application and when I type in cd ~ it doesn’t take me back to the home page. Does the cd ~ only operate theoretically?

The ~ is an alias for “wherever my home directory is”. Your terminal does this because your home could be in different places on different Operating Systems (OS). Try this:

cd ~
pwd

On a macOS computer, you’ll probably see something like:

/Users/zedshaw/

Now if you’re on a Linux computer it’d be:

/home/zedshaw/

And on PowerShell it’d be even more different depending on the Windows version you use.

Also, I think maybe you expect that cd ~ in your Terminal would also change your Finder window? There isn’t that much of a connection between the two (although that’d be kind of awesome). It’s more that, when you make changes in a directory, they’ll show up in Finder. Doing a cd to a directory doesn’t change it though, so you don’t see that happen in finder as well.

BEEP BEEP BEEP

I just wanted to tell you how much I appreciate your teaching method. It suits me perfectly.

I had purchased 2 books before yours and tried several online videos and websites Yours was the first time and place I felt I was actually leaning things from the ‘ground up’, Especially helpful was your Powershell crash course. Just beautiful.

I’m on Exercise 11 and just about to watch your video. But I wanted to tell you about trying escape sequence \a. At least on my computer, it makes a cascading chime sound when I insert it into a string and execute in Powershell. You probably already knew that. You’re so smart.

Thanks again for all the thinking you’ve obviously done on how to best teach programming.

Ahhhh, that’s called the “bell”. I guess in PowerShell it’s way more awesome than on Unix because with Unix it’s just an irritating “BEEP”.  I sometimes use that to tell me when a long running script has finished on a remote server.  I just have the script print out a large number of \a beeps at the end, and then when I hear the classic unix BEEP I know it’s done.

But, on my macOS machine I had to re-enable it to make the bell work.  I probably disabled it because the beep is annoying and the bash shell we all use loves to beep at you for everything.  Completing a file? BONK BONK BONK! Backspace one character too far?  BONK BONK BONK. To turn it on (or off, if you hate that like I do), you go to Preferences in Terminal, go to Advanced panel, and click “Audible bell” like I have here:

Anyway, you’re welcome. I’m glad you’re getting along with it well and enjoying it.  Now I’m going to go and turn off this bell so I don’t go insane.

\v \f Weirdness

This question comes from the Gitter chat so I cleaned it up into a paragraph format:

Hi all, anybody having different outputs when using escape sequences from exercise 10?
Trying to use linefeed “\f”.
I’m using ASC11 linefeed (LF) and I’m getting a female symbol instead of a return indentation.
Here’s the code I’m running:
asc11_formfeed = “Using ASC11 formfeed (FF) \f escape”
print(asc11_formfeed)
And the output is:
Using ASC11 formfeed (FF) ♀ escape
I’m using Windows 8.1, Powershell, IDEs Visual Studio Code and Atom.
I’m also having the same issue using ASC11 vertical tab (VT), and the output gives me a male symbol.
I haven’t tried it on Mac or Linux yet.

Apparently, on Windows if you use \f you get the symbol for “female”, and if you use \v you get the symbol for “male”.  I have no idea who thought that up over at Microsoft, but it’s weird that \v doesn’t produce the symbol for Venus (the “female”) symbol.  These are also super old computing characters so I have no idea why they decided to just change them after 60 years.  I’m sure someone in the comments will have a reason why.

Anyway, different terminals will have different support for these escape sequences.  These escape sequences are very old, and come from when computing was done on huge printers connected to huge computers like this one:

IBM 1403 Line Printer, Photo by waelder CC BY 2.5
By waelder – Own work, CC BY 2.5, https://commons.wikimedia.org/w/index.php?curid=1962607

Those codes were intended to move the printer head and feed mechanism around so you could do very fancy things like draw boxes around columns for you boss to get that big promotion.  These printers were a lot like a gigantic typewriter in the beginning, and then evolved to dot matrix, then laser printers, then inkjet, and now we just avoid printers completely unless we have to fax something to an ancient government office that still has one of these things in the basement doing important work.

At one point people figured out that different letters and positions on these printers created different musical tones, and would spend all night figuring out how to print Jingle Bells and other 1950s pop tunes on their printer.  That was the 1950s version of Skyrim.  I think today if our printers started making music we’d assumed the printer was hacked and throw it in the garbage.

Since nobody really interfaces with a computer through a printer these codes have become mostly obsolete except for \n, \t, and \r in some cases.  The \n is a “new line”, and on Unix that’s all you need to move the “print head” (cursor) down one line, and back to the beginning line position.  The \t does a tab, which moves the print head (cursor) to the right a certain number of spaces, usually 8 (but if someone uses Emacs then it’s random).  Finally, the \r is mostly used on Windows because they decided that \n should only go down to the next line, and \r should “carriage return” to the first position.  Basically, Microsoft was being pedantic about “carriage return, line feed” so they made everyone actually explicitly write a \n\r to get the new line.  It was one of the very earliest examples of “Well, actually, ” on record (unless you count Leibniz’s writings to Newton).

You’ll find out quite a bit of how computers work now comes from how mainframes worked in the early days of computing.  This is why Python’s (every language’s?) files seem so strange to work with, why these escape codes are so weird, and why vi is so awesome.  I’ll cover more of this in a later blog post on the history of computing that we still deal with today.

Learn Python 3 The Hard Way is now fully released and ready to download.  12 hours of video covering 52 exercises in Python 3 for beginners.  You can buy it at http://bit.ly/buylp3thw and read a free sample at http://bit.ly/lpy3thw to see if you’ll like it before you buy it.