As Linux users, it’s a special moment when we first open up a terminal and start working on the system in a way that’s most efficient, powerful, and flexible. However, your first foray into the terminal could potentially be intimidating, as all you’re greeted with is a blinking cursor and an endless world of possibilities. To help with that, we show you some Bash tips and tricks to work smarter, not harder, in the terminal.
Finding Commands with Apropos
Apropos (app-row-POE) is a command that allows you to find commands with
man or manual entries based on their description. If you’ve ever found a man page of a command, it looks a little something like this:
That “NAME” section at the top is what I’m talking about. If I wanted to find the
ping command with
apropos, I would type
apropos icmp into my terminal and hit Enter. Note that it’s not case sensitive. This pulls up every command with a NAME entry that has “ICMP” in it.
Another great use for
apropos is exploring tools that you may not be familiar with, like
selinux. Issuing the
apropos selinux command will give you a list of all the different commands you can use to interact with the SELinux, getting you started on the road to enforcing efficiently.
Substituting in the Previous Command
Something that has saved me a ton of time in the terminal is figuring out how to substitute something in the previous command. If I misspell something or just need to substitute an option in the previous command, I can use a
^ key to pull the word I misspelled, then another
^ to put in the word or option I wanted.
Let’s look at an example. Let’s say I want to ping “maketecheasier.com” to make sure I have complete Internet connectivity (including DNS). But if I misspell something, I could get some kind of error. So if I accidentally ping maktecheaser.com (missing the “i”), I’ll have some troubles.
To substitute the misspelled option, I can type
^maktecheaser.com^maketecheasier.com, and the command will run as expected. This is a simple example, but let’s say you run a long command with lots of options or misdirect the output or errors of your command. Being able to substitute
>> in a complex command is a lifesaver.
Another example is with systemd and the
systemctl command. I’ll often issue multiple different
systemctl subcommands, like start, stop, enable, or disable a service. I can just sub them out with
^start^enable, which will save me time.
This is one that is really useful in one specific scenario that happens to me a lot.
!! (bang-bang) will pull down the previous command in full. It may not seem useful, but if you think about all the times that you type a command that needs to be run under super user privileges, you’ll start to understand where this is useful.
A great example is install scripts. Let’s say you run an install script with “./SCRIPT-NAME.sh”, and it says you need to run it with super user priveleges. Just type
sudo !!, enter your password, and you’re off to the races. It saves a bunch of time, and once you get that sequence in your muscle memory, you’ll be able to do it faster than than when you were doing it wrong.
Passing Arguments from Previous Commands
!$, we can pass the last argument from a command down to the current command, and with some slight variations, we can pass any of the arguments down to our current command.
Let’s look at some examples. If I’m editing a script, I may use the command
nano samplescript.sh. Once I’m done with that script, I want to make it executable, so I may change the octal permissions to 755. To do that, I could use the
chmod 755 !$ command. Then, to pull the name of the script again, I could use
./!:2 to pull down the second argument.
Some other examples:
!^ - first argument !* - all arguments !:2-$ - second through last arguments !:2-4 - second through fourth arguments
You can substitute your numbers to pull any arguments you’d like. Bash will keep close to 100 arguments on tap with this method, and you can easily work quickly through some menial tasks like this.
Our latest tutorials delivered straight to your inbox