本文摘自:https://thenerd.academy/linux-command-syntax-arguments-options-explained/
This lesson is all about the Linux command syntax! We’re going to explore how to customize the behavior of our commands by using arguments and options. So, fasten your seatbelts and let’s jump into the action!
Linux Command Syntax - What Are Arguments?
Remember in the previous lesson when we used the ping
command to determine whether we are connected to the internet? Let’s refresh our memory by taking a look at the command we used.
ubuntu@ip-172-31-45-72:~$ ping google.com
Ping
is obviously the name of our command; google.com
is what we refer to as an argument. Arguments allow us to pass information and provide context to commands. In this example, our argument provides the ping
command the necessary information to carry out its task.
In the Linux command syntax, arguments can be either mandatory or optional. In this case, specifying a destination is mandatory because without it, the ping
command wouldn’t know which destination to communicate with.
Not all commands need arguments to function:
However, not all linux commands require arguments to function, in fact, some may not accept any arguments at all. To demonstrate, let’s take a look at a command that utilizes a different Linux command syntax and does not need any arguments to function.
Drumroll, please! Introducing the ps
command, which is short for “process status.” A process refers to an active, running instance of a program within the operating system. In simpler terms, processes are applications that are currently being executed. The ps
command conveniently displays all the processes on our computer. To see it in action, lets type ps
in our terminal and press Enter:
ubuntu@ip-172-31-45-72:~$ ps PID TTY TIME CMD 18621 pts/0 00:00:00 bash 18717 pts/0 00:00:00 ps
Awesome! Our computer’s processes are now on display. If you don’t recognize these processes don’t worry! We’ll delve deeper into them in a future lesson.
Notice that the ps
command didn’t require any arguments to function. In fact, it doesn’t accept any arguments at all. However, that doesn’t mean we can’t customize its behavior. To do so, we need to use a different Linux command syntax, with something called “options.”
Customizing command behavior with Options:
Options are also known as “flags” or “switches,” are specific letters or words that can be typed after a command to modify its behavior in some way.
For instance, by default, the ps
command doesn’t include background applications in its output. To display every process, we need to use the ps
command followed by the e
option where the lowercase e
stands for “everything.”
ubuntu@ip-172-31-45-72:~$ ps -e
Options, like the one we’re examining here, typically start with a single hyphen. This symbol denotes that we are specifying an option and not an argument. Let’s give it a try!
ubuntu@ip-172-31-45-72:~$ ps -e PID TTY TIME CMD 1 ? 00:00:15 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 rcu_gp 4 ? 00:00:00 rcu_par_gp 5 ? 00:00:00 slub_flushwq 6 ? 00:00:00 netns 8 ? 00:00:00 kworker/0:0H-events_highpri 10 ? 00:00:00 mm_percpu_wq ubuntu@ip-172-31-45-72:~$
Awesome! The list of processes now features many more applications!
Keep in mind that different commands require different options; they’re not one-size-fits-all. For example, the e
option might perform an entirely different function for another command or might not be recognized at all. In other words there is no universal Linux command syntax that works for every command.
So, how did we know that -e
is a valid option for the ps
command? Did we randomly type letters until something worked? Nope, we consulted something called the “manual pages,” but let’s not get ahead of ourselves here, we’ll explore these in our next lesson (here’s a sneak peek if you are curious).
Remember: Command Options are case sensitive:
When specifying options, it’s important to remember that they are case sensitive, meaning that -E
with an uppercase E
might not be recognized or could be misinterpreted. To demonstrate, let’s try it and see what happens.
ubuntu@ip-172-31-45-72:~$ ps -E error: unsupported SysV option Usage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text. For more details see ps(1). ubuntu@ip-172-31-45-72:~$
Uh-oh, error message! Just as we thought, the -E
option with a capital E
doesn’t exist within the ps
command.
How to specify Options with style:
Now that we’ve got a handle on options, let’s mix things up a bit. Within a command, we can specify more than one option to further customize its behavior.
To demonstrate, let’s type our command just like before but this time let’s also include the -f
option, which stands for “full format” and makes the ps
command display our processes in a format that includes even more juicy details.
ubuntu@ip-172-31-45-72:~$ ps -e -f
An alternative syntax is combining all of our options under a single hyphen.
ubuntu@ip-172-31-45-72:~$ ps -ef
This Linux command syntax makes our options look neater and makes quickly identifying them easier. With our command dressed to impress, let’s hit “Enter” and watch the magic unfold.
ubuntu@ip-172-31-45-72:~$ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Feb01 ? 00:00:15 /sbin/init root 2 0 0 Feb01 ? 00:00:00 [kthreadd] root 3 2 0 Feb01 ? 00:00:00 [rcu_gp] root 4 2 0 Feb01 ? 00:00:00 [rcu_par_gp] root 5 2 0 Feb01 ? 00:00:00 [slub_flushwq] root 6 2 0 Feb01 ? 00:00:00 [netns] root 8 2 0 Feb01 ? 00:00:00 [kworker/0:0H-events_highpri root 10 2 0 Feb01 ? 00:00:00 [mm_percpu_wq] ubuntu@ip-172-31-45-72:~$
Amazing! The output is now bursting with info about each process. Don’t worry if it seems like hieroglyphics for now; remember, you’re still learning the ropes.
Get ready for a plot twist: Option-Arguments (Parameters):
We have another trick up our Linux command syntax toolkit! Options sometimes need arguments themselves! These are referred to as “option-arguments” or “parameters.”
One example of such an option is -u
, which stands for “user.” This option allows us to view the processes related to a specific user. To specify a user, all we have to do is type its name after our option. In this case, this username serves as the argument for this option. Let’s see it in action!
ubuntu@ip-172-31-45-72:~$ ps -u ubuntu PID TTY TIME CMD 18536 ? 00:00:00 systemd 18985 pts/1 00:00:00 bash 19086 pts/1 00:00:00 ps ubuntu@ip-172-31-45-72:~$
Ta-da! Our command now only shows the applications related to this specific user.
Here’s a fun fact: In the command we just executed, we could have typed user
instead of the letter u
.
ubuntu@ip-172-31-45-72:~$ ps --user ubuntu
This is what we call the “long form” of an option. Long-form options, such as this one, are usually preceded by two hyphens instead of just one. Let’s see it in action.
ubuntu@ip-172-31-45-72:~$ ps --user ubuntu PID TTY TIME CMD 18536 ? 00:00:00 systemd 18985 pts/1 00:00:00 bash 19087 pts/1 00:00:00 ps ubuntu@ip-172-31-45-72:~$
As predicted, our command works like a charm. Some commands only accept the short form of an option, while others accept both.
Whether the long or short form of an option is required depends entirely on the specific command and how the developers structured its Linux command syntax. In our case, the ps
command accepts both the long and the short form of this particular option.
Final Thoughts:
Wow, that was a lot! If you’re feeling a bit overwhelmed, don’t worry. Remember, it’s not about memorizing every single syntax or option right away.
As you spend more time on the command line, all this will start to feel like second nature. For now, focus on getting a feel for the linux command syntax.
Fun Facts to brighten your day:
-
Remember, options are case sensitive;
ps -E
is like asking a cat to bark. Not going to happen! Butps -e
will work perfectly well! -
Think of options like a command’s wardrobe. The right outfit (option) makes all the difference!
-
Remember, the command line is not just a place where you type stuff—it’s a canvas where you paint with keystrokes. Each argument is a brush stroke, and options are your palette of colors.