Home 0P3N Blog What Really Happens When I Press Enter
Ready to Start Your Career?
Create Free Account
The Steves profile image
By: The Steve
September 18, 2015

What Really Happens When I Press Enter

By: The Steve
September 18, 2015
The Steves profile image
By: The Steve
September 18, 2015
What Really Happens When I Press Enter - CybraryPlease 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

/tmp

$ echo $PWD

$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:

sort

me

if

you

can

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

can

if

me

sort

you

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

Schedule Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry