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.
« Previous 1 2 3 4 Next »
Buy Linux Magazine
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.
News
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.