Grasping the basics of Bash

Command Structure

A typical command has three parts that appear in a set order:

  • The command – The basic action that you are performing. Sometimes, entering just a command will do something basic. For instance, if you type just cal, then the output is a calendar for the current month. More often, however, the command by itself does nothing.
  • Options, arguments, variables or parameters – Variations on what the command does. Options that were developed for Unix or by unaffiliated developers are a single letter with a hyphen in front them. Options developed by the GNU project are whole words with two hyphens in front of them. Often, a command will have one of each that does the same thing, and you can choose whether you want the speed of a single-hyphen option or the clarity of a two-hyphen option. Some options also end in a specification; for example, if you use -tvfat with the mount command, you are indicating that you want to add a device to your system that is formatted for VFAT or FAT32, the old format once used by Windows. Options vary between commands, but you will find some frequently used conventions, such as -r or R, which mean that the command affects subdirectories and their contents, and -v, which tells the command to display an explanation of what it is doing.
  • Input – The working material for a command. This may be a single file, as when you are using the rm command to delete a file. At other times, as with the mv command, it may be an original file name followed by a space and the new path for the file. It may also be a string of characters for which you are searching or a specification, such as the format used by the date command.

A few commands also have sub-commands that come directly after the command, such as the install or remove sub-commands used by apt-get, the tool used for installing packages in Debian and Ubuntu.

You'll probably notice, too, that many commands are abbreviations that describe their function. For instance, cp copies a file, and ls lists a file. This information comes in handy when you have a purpose in mind, but are unsure what command you need.

Command-Line Shortcuts

One reason that Bash is easier to use than the Windows command line is that it has several built-in tools for making life easier. These tools can be tremendous time-savers – to say nothing of memory aids.

The first of these tools is a command history, which stores the commands that you enter. You can use the Up and Down arrow keys to move through the history and save yourself having to retype long or hard-to-remember commands.

The command history includes all sorts of interesting tricks, most of which are topics for another day. However, one basic tip is to refer back to a previous entry by entering !-[number] and pressing Ctrl+j. For example, if you ran the ls command three entries ago, typing !-3 would automatically display the ls command, but not run it. Alternatively, you could use the structure ![string] to find the last entry in the history that matched the string.

Another time-saver is tab completion. When you have entered part of a path or a command, you can press the Tab key to complete it. If more than one possibility exists, Bash will display them in multi-column form, and you can continue entering letters until only one possibility remains.

For example, if you enter ch, then press the Tab key, you see a list of 22 possible completions. Continue by typing an a, and press Tab again, and the possibilities are reduced to five. Add a t and press Tab, and, this time, the only choice is chattr, whose final letters are added after your typing.

If, like many people, you are using a virtual terminal, you also have basic copy and paste functionality available. You can use the basic commands listed in the Edit menu of your terminal, but, if you prefer, you can highlight some text, then move the cursor to the position where you want to repeat the text and press the left mouse button.

Basic Navigation

When you are new to GNU/Linux, you soon realize that it has a directory structure completely different from Windows. You will probably quickly learn your way around your home directory, because you can set it up as you please, but, beyond the fact that it is a subdirectory of /home, you may have little idea of the rest of the directory tree. Fortunately, you can use the command man here to read a summary of the main directories. From that, you can learn, for example, whether a command is more likely to be in /bin or in /sbin, where only the root user can run it.

Another point that you soon learn is that your home directory is full of configuration files that start with a period and are not ordinarily listed when you run the ls command. These are not listed because most of the time you do not want them – and, perhaps, because if they are out of sight, you are less likely to tamper with them. The fact they are not listed does not mean that you cannot use the cd command to move into them, but, if you need to see their names, then ls -A will reveal them.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Introducing Bash

    Beyond all the splash screens, screen savers, and vivid rock-star wallpaper is the simple yet powerful Bash shell.

  • Command Line: Customizing Bash

    Ease into your comfort zone with these tips for customizing Bash.

  • Command Line

    A few basic tricks can liven up the command line and add a dash of color to your console.

  • Command Line: FTP and NcFTP

    FTP and NcFTP are both FTP clients for the command line. While FTP only has basic file transfer functionality, NcFTP has a whole bunch of additional features.

  • Bash Builtins

    Even beginners can benefit from a greater understanding of the Bash shell’s many builtin commands.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News