Bash is a commonly-used shell in many Linux distributions. Bash is a command interpreter.
It is a command-line–only interface containing a handful of built-in commands; it has the ability to launch other programs and to control programs that have been launched from it (job control).
We can use bash to interact with the system.
Table of Contents
Bash in Linux
Bash is a default shell now. It is very convenient. For example, they remember commands that we have typed and let us reuse those commands.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!It also let us edit those commands, so they don’t have to be the same each time. And bash let us define our own command abbreviations, shortcuts, and other features.
Bash is programmable. We can write a bash script to handle our daily work.
Whenever we find ourselves doing a task repeatedly, we should try to automate it by writing a shell script. There are more powerful scripting languages, like Perl, Python, and Ruby, but the Linux shell bash is a great place to start.
After all, we already know how to type commands.
If you are new to Linux, you can refer to this article: Linux Commands for Linux Beginners (Cheat Sheet) . This guide will equip you with the essential knowledge and skills to become proficient in using the command line interface (CLI) effectively.
Advantage of Bash
Bash is as powerful as other shells but adds convenience functions like the double brackets ([[ and ]]) in the sample code.
These “Bashisms” are much loved by Bash users because they avoid the sometimes verbose and awkward syntax in other shells like tcsh or ash.
However, they are unique to Bash and are not POSIX-compliant, which could cause compatibility issues on systems not running Bash.
Then again, Bash is open source free software, so most users can install it if they need it. The lack of compatibility only forces an extra dependency and does not exclude anyone from using a script.
How to use Bash in Linux
Most modern Linux and Unix distributions provide a Bash shell by default. They do this because Bash is well-known, and it has several convenience functions that other shells don’t.
However, some systems use another shell by default. To find out whether we are running a Bash shell, we can use the echo command along with a special variable representing the name of the currently running process:
$ echo $0
bash
Understanding Bash Script in Linux
A Bash script is a plain text file which contains a series of commands.
These commands are a mixture of commands we would normally type ouselves on the command line (such as ls or cp for example) and commands we could type on the command line but generally wouldn’t (you’ll discover these over the next few pages).
Anything we can run normally on the command line can be put into a script and it will do exactly the same thing.
Similarly, anything we can put into a script can also be run normally on the command line and it will do exactly the same thing.
Here’s a simple bash script that prompts the user to enter their name and then greets them with a personalized message:
#!/bin/bash
# Prompt the user to enter their name
read -p "Enter your name: " name
# Greet the user with a personalized message
echo "Hello, $name! Welcome to the world of bash scripting!"
Save the above code in a file with a .sh extension (e.g., greeting_script.sh). To run the script, open a terminal, navigate to the directory where you saved the file, and then execute the following command:
bash greeting_script.sh
The script will prompt you to enter your name, and after you provide your name, it will greet you with a personalized message.
Linux Bash alternatives
On Linux, the interpreter that can understand the user commands and settings and pass them on to the computer for further processing is called a shell. Here are a few shells on Linux:
- sh – The Bourne shell is the first shell to be introduced on UNIX. Bash improves upon sh. All the sh commands can also be run on bash, but not all bash commands can be used on the sh shell. This makes bash a superset of sh and other options/modules.
- csh – The C shell with a syntax similar to the C programming language.
- tcsh – The TENEX C shell is the C shell with additional features.
- ksh – The Korn shell, a command and programming language.
Check Bash version in Linux
We can use this command to check the bash version in Linux.
% bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.
Bash shortcut – Navigation
- Ctrl + a Go to the beginning of the line.
- Ctrl + e Go to the end of the line.
- Alt + f Move the cursor forward one word.
- Alt + b Move the cursor back one word.
- Ctrl + f Move the cursor forward one character.
- Ctrl + b Move the cursor back one character.
- Ctrl + x, x Toggle between the current cursor position and the beginning of the line.
Bash shortcut – Editing
- Ctrl + d Delete the character beneath the cursor.
- Ctrl + h Delete the character before the cursor (like backspace).
- Ctrl + k Cut the line after the cursor to the clipboard.
- Ctrl + u Cut the line before the cursor to the clipboard.
- Ctrl + d Cut the word after the cursor to the clipboard.
- Ctrl + w Cut the word before the cursor to the clipboard.
- Ctrl + y Paste the last item to be cut.
Bash shortcut – Processes
- Ctrl + l Clear the entire screen (like the clear command).
- Ctrl + z Place the currently running process into a suspended background process (and then use fg to restore it).
- Ctrl + c Kill the currently running process by sending the SIGINT signal.
- Ctrl + d Exit the current shell.
Bash shortcut – History
- Ctrl + r Bring up the history search.
- Ctrl + g Exit the history search.
- Ctrl + p See the previous command in the history.
Related post: