What Really Happens When I Press Enter

September 18, 2015 | Views: 1536

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Please note: some of the backslahes ( \ ) in the coding below may have been removed by the WordPress program.

When you press enter, the program doesn’t just run. There’s an order of operations that takes place. Understanding this order can keep you from making some pretty big mistakes. Let’s take a command and break apart what really happens when we press enter.

$ echo “The current directory is $PWD and I can count to 4.” \ (hit enter)
> {1..4}

The current directory is /tmp and I can count to 4. 1 2 3 4

When we hit enter, the first things that are processed are meta characters. What are meta characters? They’re characters that have special meaning.

The first meta character we have is the $. This let us know we’re going to be doing some kind of substitution. In this case, we’re saying replace PWD with the value of the variable.

PWD is a special environment variable that’s holds your current working directory. When BASH sees the $ it then looks for the variable PWD and replaces it with the value stored inside. You can also do nested commands with this, but that’s another lesson.


The next meta character we have is the “.” It will “escape” meta characters. When we say escape, we mean instead of the meta character serving a special purpose, it’s taking literally. For example:

$ cd /tmp

$ echo $PWD


$ echo $PWD



Notice, this time we literally print $PWD instead of doing substitution. When we use this at the end of a line, it allows us to continue typing the command. It’s escaping the enter key.

Just as a note, you can also put meta characters inside single quotes and it will be taken literally as well. Give it a try.


The next part of the command starts on the second line. For the last part, we do brace expansion. You can represent a range inside of curly braces. Then, that will be the same as if you typed in out. {1..4} is replaced with 1 2 3 4 before the command ever runs. That may seem like a weird concept, so let’s try this.

$ echo {1..4}

1 2 3 4

$ echo {fr,b,t}ed

fred bed ted

This is the same as if I had literally typed out ‘echo 1 2 3 4’ or ‘echo fred bed ted’. This is a great way to generate very complex strings very quickly.


After all of our meta characters have been dealt with and any substitutions or expansions have taken place, the next thing to happen is redirection is setup. Let’s take a look at the sort command. I made a file called /tmp/words.txt that contains the following text:






If you needed to sort this file and did not need to keep the unsorted version (bad idea – always save a backup), you might try to run the following command.

$ sort /tmp/words.txt > /tmp/words.txt

$ cat /tmp/words.txt

The file is empty. On paper this, command looks like it should work. The sorted text will be sent out on channel 1 and overwrite the old file. After all, of our meta characters are dealt with redirection is setup.

If we use a single > the file contents are removed so the new output can be placed inside. The file is empty before the sort command is ever run. To make this work, we have to turn it into a 2 step process.

$ sort /tmp/words.txt > /tmp/sortedwords; mv -f /tmp/sortedwords /tmp/words.txt

$ cat /tmp/words.txt






The ; is used to separate commands. We ran two commands on a single line. Using the ; to separate them, they’re both run back to back. This time you will be left with a sorted file. If the file is empty, it means you didn’t put content back in.


Play with this a bit to make sure you understand what order things are happening in. For example, what will happen with this command?:

$ echo “test” ; > $PWD/test.txt

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. Very likely the \ characters were removed automatically by wordpress as they’re considered “special” characters. We simply have to replace every instance of “\” with “\”


  2. @fiend138, we did not remove anything from the original submission. If you have content that needs to be edited/added, please send me a private message.

  3. Actually they removed all of the \ from the document. :-/ Well that pretty much makes half of what I say incomprehensible

  4. The very first command is listed wrong for some reason. It should be…

    $ echo “The current directory is $PWD and I can count to 4.” \ (hit enter)
    > {1..4}

    Not sure what happened there.

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?