Anatomy of a Command

by benjamin@tty1.blog at 2023-03-25T19:26:31.000Z

For the most part, this blog will not be dealing with the very basics. I find it safe to assume that most people interested in a blog about the Linux terminal already have some degree of fundamental knowledge about it.

That said, it's a good idea to have a grounding place we can all start off from. Let's take a command and dissect it, looking at its parts and learning what they mean.

Anatomy of a Command

Lets start off with this simple command:

$ ls --almost-all --sort=extension ~/downloads

Whoa, whoa, whoa, what's going on here? So much. If you're not very used to the terminal, this is a perplexing line of gibberish. Let's break it down.

$ - The Prompt

See the $ at the beginning of the line? That's what's known as the shell "prompt." Prompts come in all different flavors, but there's one thing that remains consistent between most default prompts, by convention:

Usually, your prompt will be more complex, something more like this:

benjamin@archimedes:~/projects/tty1.blog] $

A quick rundown of what's going on here:

Basically, a prompt is meant to give you information to help contextualize commands you run.

For tty1, I will use the following convention:

Keep in mind: the prompt is not part of the command you run. If you're copying a command, don't include the starting $ in what you run or you'll get an error.

ls - The Command

While the whole line you enter in is often referred to as a command, the actual command itself is just the program being run. In this case, that's ls, a command which lists files and directories in a directory ("directory" is what most people know as a folder).

~/downloads - Arguments

An "argument" is a piece of information you give to a command to help it do its job. Each command will take a certain number of arguments; ls takes one, the path.

Giving a command an argument is called "passing" it the argument. By passing ls (which lists the files and directories in a directory) the ~/downloads path, you're telling it to list for you every file and directory inside the ~/downloads directory.

For ls, this argument is optional; if you'd left it out, by default it would assume you want a list of the files in the folder you're currently inside, the "working directory".

Commands will sometimes have multiple arguments, in which case they're separated by spaces.

--almost-all --sort=extension - The Flags

Flags specify options or settings to use when running the command. In this case:

Confused yet? There's more. Most programs have multiple versions of each flag. While each command can do this differently, usually flags starting with -- are "long" flags and ones starting with - are short ones which are easier to type out. For example, in the ls command, -A means --almost-all and -X means --sort=extension. With that in mind, we could have written our command this way:

$ ls -A -X ~/downloads

In fact, you can go even shorter. With one-letter short flags, you can combine them like so:

$ ls -AX ~/downloads

That means the same thing as our original command:

$ ls --almost-all --sort=extension ~/downloads

While short flags are far quicker to type out, I'll avoid using them in this blog, since they're confusing and less understandable than long flags if you don't know what's going on.

Options should be listed before the arguments of the command.

Getting Help

You know how commands work! Well, you know the basics. You're almost certainly going to run into problems and issues, especially since I won't usually be sticking to the basics like I do in this article. Here are some resources that can help with that:


Benjamin Hollon

benjamin@tty1.blog

Benjamin Hollon is a writer and citizen of the world with far too many hobbies. Besides writing for his blogs, he codes, plays and composes music, writes poetry and fiction, and more. He is currently studying Communications and Professional Writing at Texas A&M.

He is active on Mastodon.

Email Public Key


Comments

Comment via Fediverse



Liked what you read?

What's next?