Understanding bash Command Syntax and Structure

Many people happily skip through their Linux use without understanding the fundamentals of commands in the bash shell. Note that this approach makes you lose out on some cool capabilities available in bash. The more you know about how this shell’s “language” works, the more interesting things you can do with it.

The basics of using bash at the command prompt often involve typing a command and any of its flags and values. For example, you enter the ls -la ~ command to see a long-format listing of all files in your home directory, including those that start with a dot (.), which are hidden files. That other mysterious squiggle character is technically called a tilde. The tilde is a bash shortcut character that points to a user’s home directory. For this example, I merely list the contents of my home directory.

You can break a command into three distinct components:

The command name
The options or flags
The arguments

Consider this example.

Start with a simple command. The du command lists the contents of the directory you’re now in, and its subdirectories, and how much hard drive space each item takes up, with a total at the end. Try typing just the du command by itself:

 du                                                                  

That’s neat, but it probably raises more questions than it answers. The output gives you a long listing of data, but of what? Are those numbers in bytes, kilobytes, or messages from outer space? To clarify, try adding a simple option to your command:

 du -h                                                               

Get Linux® For Dummies®, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.