- Unix Commands Reference
- Unix Commands - Home
bash Command in Linux
Bourne Again Shell, commonly referred as bash is a popular scripting language and command line shell used as a default shell on most Linux operating systems and macOS. bash provides an interface that allows users to interact with the operating system and run commands or write scripts.
bash is designed to be compatible with the original UNIX shell, often referred as sh. It incorporates features from other shells such as the Korn shell (ksh) and the C shell (csh). You can use bash to run programs, manage files and automate tasks on your system. You can customize your bash environment by defining aliases, setting environment variables and creating shell scripts.
Table of Contents
Here is a comprehensive guide to the options available with the bash command −
- How to Install bash in Linux?
- Syntax for bash in Linux
- Different Options Available for bash in Linux
- Basic bash Commands in Linux
How to Install bash in Linux?
By default, bash is the default shell on most Linux operating systems. However, in case the shell is accidentally removed, you can reinstall the bash shell from the following command −
sudo apt install bash
The above command will work on Debian based Linux distributions like Ubuntu, Linux Mint and other such operating systems.
The CentOS, RHEL and Fedora users can install bash on their systems from the terminal through any of the following two commands −
sudo yum install bash sudo dnf install bash
To confirm bash is successfully installed on Linux, you can run the below-given command −
bash --version
To check whether you’re currently in a bash shell on Linux, you can use the following command −
echo $0
If the result shows something like bash, it confirms that you are using the bash.
In case you are using any other shell and want to switch to bash, you can execute the below-given command −
chsh -s /bin/bash
Syntax for bash in Linux
The general syntax for bash in Linux is provided below −
bash [options] [command_string | file]
- [options] − These are optional flags or options that modify the behavior of the bash shell.
- [command_string | file] − You can either provide a command directly (as a string) or specify a script file to execute.
Different Options Available for bash in Linux
With bash, you can use different options, which are provided in the table given below −
Option | Description |
---|---|
-i | Interactive mode (allows direct input) |
-c | Execute a command string directly |
-n | Syntax check (no actual execution) |
-e | Exit on error (terminate if any command fails) |
-x | Debug mode (print commands before execution) |
-l | Login shell (sets environment variables) |
-r | Restricted mode (limits certain features) |
-s | Read commands from standard input |
-v | Print shell input lines as they are read (useful for debugging) |
-D | A list of double-quoted strings preceded by a dollar sign ($) will be displayed on the standard output |
There is also a long list of bash options you can explore apart from basic options, such as multi-character options, arguments, invocation and other such options. You can get a detailed overview of these options by opening the bash manual on the terminal using the below-given command −
man bash
Basic bash Commands in Linux
The following are some basics bash commands that are widely used in bash shell −
File and Directory Management
You can use the following commands to perform basic file and directory management tasks −
ls Command
The ls command is a widely used command on Linux systems supported by bash that lists all files and directories in the current directory location. With this command, you will get valuable information about your files and directories. This information includes their names, permissions and other information.
The basic syntax to use the ls command on Linux is provided below −
ls [options] [file|dir]
You can use different options with the ls command, these are discussed in the table below −
Option | Description |
---|---|
-l | Long format (displays additional information like permissions, owner, size, and modification time). |
-a | Shows hidden files (those starting with a dot). |
-t | Sorts files by modification time (newest first). |
-r | Reverses the order of listing (useful with -t). |
-h | Human-readable file sizes (e.g., 1K, 2M). |
-d | Lists directories themselves, not their contents. |
-R | Recursively list subdirectories and their contents. |
For example, to list files of a home directory on Linux in bash, you can use the following command −
ls
cd Command
cd command is another useful command in bash that changes your current working directory. With the cd command, you can move to different locations of your system right from the terminal. The basic syntax to use the cd command is provided below −
cd [directory]
Here, you have to specify the directory according to your choice. You can also use options with the cd command, the most commonly used options are provided in the table below −
Option | Description |
---|---|
.. | Move you back to the previous directory |
~ | Move you back to the user’s home directory |
For example, to navigate to a directory Documents, use the following command −
cd Documents
pwd Command
The pwd command is used when you want to know your current working directory location on the terminal. The syntax to use the pwd command in Linux is given below −
pwd [options]
Here, you can use several options with the pwd command, the most commonly used options are listed in the table below −
Options | Description |
---|---|
-L | Follows symbolic links (symlinks) and resolves the target path. Useful when you want to traverse symlinks and access the actual file or directory they point to. |
-P | Does not follow symbolic links. It treats symlinks as regular files or directories. You can use this option when you want to work directly with symlinks themselves, not their targets. |
The following example uses the pwd command without an argument to know the current working directory −
pwd
File Manipulation
File manipulation process involves creating, copying, moving, deleting, renaming, searching and modifying files. In bash, you can easily manipulate files using different commands, which are discussed below −
touch Command
With touch command, you can create your files within a seconds directly from the terminal. However, the touch command only allows you to create an empty file on your system with your desired file name. The syntax to use the touch command in Linux is provided below −
touch [file-name]
The common options used with the touch command are given below −
Option | Description |
---|---|
-a | Update the access time of the specified file to the current timestamp. |
-m | Update the modification time of the file to the current timestamp. |
Let’s create a file name sample.txt using the below-given touch command in bash −
touch sample.txt
cat Command
You can use the cat command in bash shell to view the content inside the file without having to open it for editing purposes. The syntax to use the cat command on Linux is given below −
cat [options] [file path]
With cat, you can also use several options, which are discussed below −
Option | Description |
---|---|
-n | Prefixes each output line with its line number |
-b | Numbers only non-blank output lines |
Let’s view the content inside the text file named file.txt using the following cat command −
cat file.txt
rm Command
The rm command is another widely used command in bash that allows you to remove a specific file or directory from your system. The syntax to use the rm command in Linux is as follows −
rm [options] [file|dir path]
You can use different options with rm command, the widely used options are discussed in the table below −
Option | Description |
---|---|
r | Removes directories and their contents recursively. |
f | Forces the removal of a file without asking for confirmation. |
d | Removes empty directories. |
For example, to remove a file let’s say sample.txt from the system, the following command should be used −
rm sample.txt
mkdir Command
mkdir command is used in bash to create a new directory on your system. For that purpose, you have to specify the directory name that you want to create. The syntax to use the mkdir command is provided below −
mkdir [options] [dir]
The commonly used options with the mkdir command are provided in the table below −
Option | Description |
---|---|
-m | Set specific access permissions (mode) for the newly created directory |
-p | Enable the creation of parent directories if they don’t already exist. |
Let’s create a directory named new_dir using the following command −
mkdir new_dir
cp Command
You can use the cp command in bash to copy files and directories on your system. The syntax to use the cp command on Linux is given as under −
cp [options] [source_path] [destination_path]
Here, are some commonly used options with cp command −
Option | Description |
---|---|
-r | Copy directories recursively, preserving their structure. |
-p | Preserves the file’s mode, ownership, and timestamps during the copy. |
-i | Prompts before overwriting an existing file in the destination. |
For example, to copy a file called file.txt to a directory named Documents, you can use the following command −
cp file.txt Documents
mv Command
You can move your files and directories in bash shell by using the mv command. The syntax is similar to cp command besides replacing the cp with mv keyword, as given below −
mv [options] [source_path] [destination_path]
The following are some commonly used options with the mv command −
Option | Description |
---|---|
-n | Prevents overwriting existing files in the destination directory. |
-i | Prompts you for confirmation before overwriting any existing files. |
-b | Creates a backup copy of any existing file in the destination. |
Let’s move a file named myfile.txt to Downloads directory using the following command −
mv myfile.txt Downloads
Archiving and Compression
In bash, you can also archive and compress files directly from the terminal using different utilities, which are discussed below −
tar Command
The tar command is used for creating and extracting archive files (often called “tarballs”). It combines multiple files into a single archive. To create an archive −
tar -cvf my_archive.tar file1.txt file2.txt
To extract the archive file −
tar -xvf my_archive.tar
The commonly used options with the tar command is provided in the table below −
Option | Description |
---|---|
-c | Create an archive |
-x | Extract file from |
-v | Verbose mode (show progress) |
-f | Specify the archive filename. |
zip Command
The zip command compresses files into a ZIP archive. To create a ZIP file −
zip my_archive.zip file1.txt file2.txt
To extract the zip file, you can use the following command −
unzip my_archive.zip
Viewing File and System Details
You can also view file and system details in bash shell by using commands like echo, df and uname. The details are provided below −
echo Command
The echo command is a basic and frequently used command in Linux that allows you to display text or messages on the terminal. The basic syntax to use the echo command on Linux is provided below −
echo [options] [ARGUMENTS]
The common options used with the echo command is provided below −
Option | Description |
---|---|
-n | Suppress the trailing newline (useful for formatting). |
-e | Interpret escape characters (e.g., \n for a new line). |
The following example display a text Hello Linux Community using the echo command −
echo "Hello Linux Community"
df Command
The df command in bash provides information about file system disk space usage on mounted file systems. It helps users check available space and monitor disk capacity. The syntax to use the df command in Linux is given below −
df [options] [/dir]
Common options includes with the df command is provided in the table below −
Option | Description |
---|---|
-h | Display sizes in human-readable format (e.g., 1kb, 1Mb, 1Gb). |
-a | Show all available information. |
-m | Display size in megabytes. |
-k | Display size in kilobytes. |
-i | Show available inodes (for Linux and Unix). |
Let’s find out the disk information on Linux using the following command −
df -h
uname
You can find essential information about your system on bash directly by running the uname command. This information includes kernel name, operating system, kernel version and more. The syntax to use uname command on Linux is given below −
uname [options]
The following options can be used with the uname command −
Option | Description |
---|---|
-a | Displays all available information, including the kernel name, network node, kernel version, hostname, kernel release date, machine hardware name, hardware platform, and operating system. |
-s | Shows only the kernel name. |
-r | Prints the kernel release version. |
-n | Displays the network (domain) name of the machine (current computer). |
To find out all available options, let’s use the below-given command −
uname -a
Control Operations
To control different operations on Linux from the bash shell, you can use commands like kill, history and pipe, which are discussed below −
kill Command
The kill command is a powerful command used in bash for sending signals to processes and terminating it gracefully or forcefully. You can use the kill command on Linux using the following syntax −
kill [options] PID
Common options used with the kill command are provided in the table below −
Option | Description |
---|---|
-9 or SIGKILL | Forcefully terminate the process (use with caution). |
-15 or SIGTERM | Gracefully ask the process to terminate. |
For example, to kill a process with PID 8857, you can use the below-given command −
sudo kill -9 8857
history Command
The history command is used in bash to display your command history. It is useful for recalling previous commands and their execution order. The syntax to use the history command is given below −
history [options]
The following are some common options that you can use with the history command on your bash shell −
Option | Description |
---|---|
-c | Clear the history |
-n | Display line numbers |
-a | Append new commands to the history file. |
Let’s clear the history on bash shell using the following command −
history -c
pipe Command
The pipe operator (|) is used in bash shell to combine commands, as it takes the output of one command and uses it as input for another. For example, if you want to list files and filtering for those containing the file.txt, you can use the following command −
ls -l | grep "file.txt"
Bonus Tip − You can also use the bash command to execute a script with the .sh extension. For example, the following command will execute the script file.sh on Linux −
bash file.sh
That’s how you can use bash to perform multiple tasks on your Linux systems.
Conclusion
Bash is a powerful command-line shell and scripting language that serves as the default shell on most Linux distributions. With bash, you can run commands and execute scripts efficiently.
In this tutorial, we covered the method to install bash on a Linux system, explored its syntax and options, and provided examples of various commands that can be executed in the bash shell. By following this guide, you will be well-equipped to navigate and utilize bash effectively.