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.

Learn Elixir The Beast Mode Way

I’ve been meaning to start learning a new language for a series of books after Python.  My list is pretty long, including Nim, Elixir, Go, Rust, JavaScript, and I even might, just maybe, who knows, do a PHP7 book.  Currently though I have to do three more courses covering Python before I can move on to the next book, but I do want to start getting off Python for my business software soon.

The first thing I’d like to replace is my current Django web stack.  It works, and powers my things, but honestly it’s not the greatest functionality.  I’d love to just code a replacement in anything else, but my time is limited.  I have to work on books right?  Gotta record those videos.

A while back I heard that there’s people who hate my books for total beginners because they are too repetitive and slow.  Alright, sure, if it’s going too slow then chances are it’s not the right book for you.  I actually admit that the book is not for people who can’t handle doing some rote work or are already experts.  No book can perfectly train everyone, and it’s insane to expect my book aimed at a person with zero knowledge to also train everyone else.

But, I’m a problem solver, and I like to solve my problems by combining many things at once.  Solving one problem is boring.  I need to do three at once:

  •  I have a problem that I’d like to learn a new language to do some web development and replace my current Django stack.
  • I also have the problem that I need to learn a new language for my next book.
  • I then have this problem that people who feel my beginner books are beneath them seem to think the books don’t work for anyone else.

I believe there is a class of person who feels they can’t learn by practice, but only that they can learn by “building stuff”.  I don’t really write books for them, but there was a tickle in the back of my brain that said, “You sure?”

Then it hits me!  I got my next book Learn More Python 3 The Hard Way in the works, I need to learn Elixir, well why don’t I just try to do the Learn More exercises in Elixir as my way to learn Elixir?  Then when I’m done I’ll have learned enough Elixir to work on my own site, and then I can probably do a Learn Elixir The Hard Way, and then…

Wait! What if I do a “Learn Elixir The Beast Mode Way”, or probably a better title because that’s super weird, but I think you know what I mean.  What if I take the projects in Learn More Python The Hard Way, do them in Elixir, then add a large initial “crash course” that teaches enough Elixir to make you dangerous enough to do the projects?  Then, the only structure is the projects, and you can do those in almost any order you want.

I’m actually very into this idea now.  I freely admit that when you’re an expert that rote practice style of learning isn’t very useful.  Learn More Python is kind of the list of projects I work through when I’m learning a new language, so why not just do them with Elixir too?

Starting maybe tomorrow or next week (time permitting) I’m going to “beast mode” Elixir using the projects from my Learn More Python The Hard Way and probably find a place to post the results.  I think what I’ll do is go through this awesome Elixir School website as the fast crash course part, then I’ll start going through my book using Elixir.  If it works then this may become the new format for future books aimed at people who aren’t total beginners.

Q: What’s a best first language?

I have purchased LPTHW, but was hoping I old ask you for some general advice, I hope that is okay.

I am one of those people who would like to learn programming as a complete novice, but keeps going round in circles with how to start.

I started LPTHW, and as I continued to research into first language to learn, I came across an article where a university professor said that python being multi paradigm is not a good first language, and instead recommended Smalltalk. I then researched into this and found some resources on Squeak.

However I’m struggling with learning something for the sake of it, so decided a project to work would be a lot more motivating. If I went into this area as a job I would be more likely to choose web development than software, and [realized] javascript is a language that would be really useful.

However I’ve seen many people online who have said it is a bad language and can make follow up languages hard to learn.

What is your opinion on this, and how much in the way of bad habits do you think it installs?

Well, university professors in computer science are notorious for being completely clueless about teaching beginners. Most of their advice is based on teaching only people who’ve been coding since they were 12, and most of them have no training in educational theory. In many cases they even have super weird ideas that have no basis in any research. In this case I’d say you ran into this type of professor and should just ignore the advice.

Look, you’re probably like many people who are looking for the “best” first language because you think of learning a programming language is like investing in a stock. You expect, after X months, to receive a Y benefit from the investment. If you don’t then you get disillusioned. You are probably also hoping that you only have to learn one language because learning new languages is difficult, so you want to make sure you pick the best one and only language.

Programming languages are not like investments, but more like a vacation. You don’t go on a vacation to Bali and say, “Alright, if I spend $10k and 3 weeks in Bali I expect my return to be $20k/year.” Instead you happily spend the $10k and walk away with photos, memories, and experiences you can use in your life after. You also don’t go, “Ok I have only one vacation place to choose from for the rest of my life so I better pick the best one. Is Bali the best?” Instead you go to multiple places during your life to gather experiences, and each one costs you, but the return is intangible.

Programming languages are like vacations in that you aren’t investing in something to get a return, and you aren’t going to learn just one. The right attitude is to enjoy your vacation in Python and come away with knowledge and experience to use in your daily life, then go learn another language. Try to learn 4, so that you’re experienced at learning languages. If there’s one thing that never changes about programming it’s that you have to keep learning new programming languages.

So, get back to learning Python. My book guides you through it. Nothing else matters until you’re done with that.  After that try building some things with Python.  Nothing fancy, just small little projects and hacks so you can have experience using Python.  After that, learn 3 more languages if you can.  It seems that after the 4th language it becomes easier to learn more and you’ll also feel more competent in programming.

One final note: If you’re the type of person who hits adversity then tries to find a way around it, you’ll need to stick with my book and resist that temptation. I put points in my book where it gets more difficult, but all of those points are solvable if you just stick with it and print out variables. I’m not kidding. Print print print. If you do that, then you’ll crack it. If you go “Oh this is hard, well I work better when I have something to work on so I’ll go switch to Elixir and build a Facebook” then when you hit problems doing that you’ll do it again and again until you never do anything.

Stick with it. Fight the adversity, and stop jumping around.