The “bash” Command in Linux [5 Practical Examples]

The bash is the short form of “Bourne-Again Shell.” It is a Unix shell. It enables users to interact with the Linux Operating System through commands. Bash is a command language interpreter as well as a programming language. Like other programming languages, it supports starting a shell from the beginning and creating a new login shell. The bash command in Linux can also read and execute commands from a shell script file.

A. Description

The bash command in Linux is a popular tool for starting a shell from the beginning, creating a new login shell, running a bash script from the terminal etc.

B. Syntax

The bash command in Linux is a command that takes OPTIONS and FILE as its argument. The syntax for bash commands is

bash [OPTIONS] [COMMAND_STRING / FILE]

Note: Here, the OPTIONS and COMMAND_STRING / FILE are enclosed by a square bracket, meaning that OPTIONS and COMMAND_STRING / FILE are not mandatory for the command.

C. Options

The option can be added to the syntax of the bash command to modify the command. I have listed some valuable options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go man page, type the following command and press ENTER.

man bash

Useful Options

  • -c: (It creates a new shell different from the current shell and performs the later task on the new shell)
  • -i: (Makes the shell interactive.)
  • -l: (Make bash act as if it had been invoked as a login shell)
  • -r: (Makes the shell restricted)
  • -v: (Makes shell print input lines as they are read.)
  • -x: (Prints commands and their arguments as they are executed)

Note: All options in Linux are case-sensitive. So, You must be careful while using these.

Practical Examples of the “bash” Command in Linux

The bash command in Linux is a versatile tool for starting a shell from the beginning and creating new shells. The bash command in Linux has many practical applications, and a few of them are outlined below.

Example 1: Starting a New Shell Session Using the “bash” Command in Linux

You can start a new shell session using the bash command in Linux. When a new shell session starts, it reads the ~/.bashrc file. Here, I will start a new shell session. To accomplish this task, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano ~/.bashrc

➌ Now, tap the ENTER button.

➍ After that, navigate to the end of the file and type the following text line.

echo "New shell started"

New shell started is written at the last line of ~/.bashrc file

➎ Now press CTRL+S then CTRL+X

➏ Then, type the following command

bash

➐ Now, push the ENTER button.

Output >

A new shell session starts by executing the ~/.bashrc file. And previously, I had added a command to print a message while executing that file. That is why the line “New shell started” is printed on the terminal, as shown in the following image.The bash command in Linux is used to start a new shell session by executing the ~/.bashrc file.

Example 2: Starting New Login Session Using the “bash” Command in Linux

You can start a new log-in shell session using the bash -l command in Linux. When a new log-in shell session starts, it reads the ~/.profile file. Here, I will create a new login session. To accomplish this task, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano ~/.profile

➌ Now, tap the ENTER button.

➍ After that, navigate to the end of the file and type the following text line.

echo "New login session"

New login session is written at the last line of ~/.profile file

➎ Now press CTRL+S then CTRL+X

➏ Then, type the following command

bash -l

➐ Now, push the ENTER button.

Output >

A new login session starts in Linux by executing the ~/.profile file. And previously, I had added a command to print a message while executing that file. That is why the line New login session is printed on the terminal, as shown in the following image.The bash command in Linux is used to start a new login session by executing the ~/.profile file.

Note: Here bash -l command executes the ~/.profile file, whereas the bash command executes the ~/.bashrc file.



Example 3: Writing a Bash Script and Running it from the Terminal

You can have a .sh file containing bash script commands and run these commands from the command line. Here I have a .sh file containing some bash commands. I will run these commands from the command terminal. Proceed as outlined below to accomplish this.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Enter the command into the command line.

nano greetings.sh

➌ Now, hit the ENTER button.

➍ Type the following text lines

#!/bin/bash

echo "Hello There!"

echo "Welcome to LinuxSimply!"

echo "Today's Date: $(date)

➎ Then, press CTRL+S and CTRL+X sequentially.

➏ Type the following command in the command prompt:

bash greetings.sh

➌ Now, press the ENTER button.

Output >

The bash command in Linux runs bash script commands written inside a .sh file from the command terminal, as depicted in the following image.Bash scirp is read and run by using the bash command in Linux

Example 4: Executing Command in a Different Shell from the Current Shell

The -c option of the bash command in Linux can execute the command in a different shell from the current shell. Here will assign a value to the variable sw and try to print the value of sw variable. To accomplish this, follow the steps described below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sw=501

➌ Now, push the ENTER button.

➍ Type the following text line at the last of the file opened.

bash -c echo $sw

➐ Now, push the ENTER button.

Output >

At first, “501” is stored at the sw variable. Then bash -c command starts a new shell where the echo $sw command has been executed. As this sw variable only exists in the current shell, you’ll not see any output in that different shell.the sw variable only exists in the current shell, that is why you'll not see any output in the different shell.

Example 5: Redirecting Output of a New Shell into a File Using the “bash” Command in Linux

You can redirect the output of the bash command in Linux into a separate file. Here, I will run the ncal command in a different shell and redirect the output into a separate file named ncal.txt

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the command as written below to the terminal.

bash -c ncal > ncal.txt
EXPLANATION

Explanation: Here, the bash -c command runs the ncal command on a new shell and redirects the result into a file named ncal.txt

➌ Now, push the ENTER button.

➍ Type the following text line on the command prompt.

cat ncal.txt

➐ Now, push the ENTER button.

Output >

In the following image, at first, you will notice that the bash -c command runs the ncal command on a different shell and then redirects the result into a file named ncal.txt the bash command in Linux is used along with -c to run the ncal command on a different shell and redirects the result into a file named ncal.txt

Conclusion

In this article, I have demonstrated the deployment and potency of the bash command in Linux. In the world of the Linux operating system, many things remained unexplored. After going through this article, I hope you’ll be competent enough to explore more things about the bash command with the help of the illustrated practical examples.


Related Articles

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment