Bash Overview with Best Keyboard Shortcuts
Shells are empowering. Still, most people actually fail to recognize the full power of shells and what they have to offer. There exist many shells like Bash, sh, csh, tcsh.
Whenever we login at the command line or launch a terminal window on Linux, the system launches the shell program. Shells offer a standard way of extending the command line environment. Shells can be swapped out easily and we use either bash, ch or tcsh shell easily.
The very first shell environment was the Thompson Shell, developed at Bell Labs and released in 1971. Shell environments have been building on the concept ever since, adding a variety of new features, functionality, and speed improvements.
For example, Bash offers command and filename completion, advanced scripting features, a command history, configurable colors, command aliases, and a variety of other features that weren’t available back in 1971 when the first shell was released.
Command History
With a simple command “history”, we can look at all the commands we’ve run.
Clearly, bash saves a lot of the commands in the history file.
It remembers the commands we type and stores them in a history file. You probably know a few basics of the bash history, but it’s a lot more powerful than we might realize.
Keyboard Shortcuts
With bash, it is easy to scroll through the bash history. Use these shortcuts and commands you’ve previously used will appear in the terminal.
- Up Arrow or Ctrl+P: Go to the previous command. Press the key multiple times to walk backwards through the commands you’ve used.
- Down Arrow or Ctrl+N: Go to the next command. Press the key multiple times to walk forwards through the commands you’ve used.
- Alt+R: Perform a reverse search. As shown,
Here we started writing ‘elastic’ and the last command to have this text appears. We can simply hit Enter to run that command.
We can also search history using grep. Let’s see an example here:
Run commands from History
Bash can easily “expand” previous commands, or expand them and modify them. This feature is known as history expansion and uses an exclamation mark, also know as a bang. Just type them at the prompt and hit Enter to run them like you’d run any other command.
To run a specific command from your history by its number, use the following command:
!#
For example, let’s say we wanted to run the 10th command from the command history. That’s the command with a “10” to the left of it when you run the history command. We’d type the following command:
!12
Clear Command History
The bash shell stores the history of commands you’ve run in your user account’s history file at ~/.bash_history by default. For example, if your username is shubham, you’ll find this file at /home/shubham/.bash_history.
Because your history is stored in a file, it persists between sessions. You can run some commands, sign out, come back the next day, and those commands will still be in your history file ready to view and use. Each user account has its own history file with a separate command history.
To clear your bash history, you can run the following command. This removes the contents of the user account’s .bash_history file:
history -c
Bash only remembers a limited number of commands by default, preventing the history file from growing too large. The number of history entries bash remembers is controlled by the HISTSIZE variable. The default is usually 500 or 1000 entries. You can run the following command to view the size of the bash history on your system.
echo $HISTSIZE
To set your history to zero, run the following command.
HISTSIZE=0
For the current session, bash won’t store any history entries unless your run a command like HISTSIZE=1000 to set it back to a certain number of entries.
Moving the Cursor
Use the following shortcuts to quickly move the cursor around the current line while typing a command.
- Ctrl+A or Home: Go to the beginning of the line.
- Ctrl+E or End: Go to the end of the line.
- Alt+B: Go left (back) one word.
- Ctrl+B: Go left (back) one character.
- Alt+F: Go right (forward) one word.
- Ctrl+F: Go right (forward) one character.
- Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.
Deleting Text
Use the following shortcuts to quickly delete characters:
- Ctrl+D or Delete: Delete the character under the cursor.
- Alt+D: Delete all characters after the cursor on the current line.
- Ctrl+H or Backspace: Delete the character before the cursor.
Fixing Typos
These shortcuts allow you to fix typos and undo your key presses.
- Alt+T: Swap the current word with the previous word.
- Ctrl+T: Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
- Ctrl+_: Undo your last key press. You can repeat this to undo multiple times.
Tab Completion
Tab completion is a very important bash feature. While typing a file, directory, or command name, press Tab and bash will automatically complete what you’re typing, if possible. If not, bash will show you various possible matches and you can continue typing and pressing Tab to finish typing.
- Tab: Automatically complete the file, directory, or command you’re typing.
For example, if you have a file named really_long_file_name in /home/shubham/ and it’s the only file name starting with “r” in that directory, you can type /home/shubham/r, press Tab, and bash will automatically fill in /home/shubham/really_long_file_name for you. If you have multiple files or directories starting with “r”, bash will inform you of your possibilities. You can start typing one of them and press “Tab” to continue.
So be sure to give some of these are try and speed up your command line work. Did we miss any? Let us know your favorite shortcut in the comments below.
Recent Stories
Top DiscoverSDK Experts
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment