FUNDAMENTALS A Complete Guide for Beginners
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"
➎ 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.
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"
➎ 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.
Note: Here bash -l command executes the ~/.profile file, whereas the bash command executes the ~/.bashrc file.
- The “alias” Command in Linux [3 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
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.
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.
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: 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
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
- The “echo” Command in Linux [7 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “declare” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “source” Command in Linux [3 Practical Examples]
- The “hash” Command in Linux [6 Practical Examples]
- The “read” Command in Linux [8 Practical Examples]
- The “set” Command in Linux [6 Practical Examples]
- The “unset” Command in Linux [2 Practical Examples]
- The “eval” Command in Linux [4 Practical Examples]