- What Is Bash in Linux?
- Bash in Linux
- Advantage of Bash
- How to use Bash in Linux
- Understanding Bash Script in Linux
- Linux Bash alternatives
- Check Bash version in Linux
- What is Bash in Linux? [Running, Finding & Making]
- What is Bash in Linux?
- 1. Bash as a Shell
- 2. Bash as a command
- 1. Bash as a Command
- 1.1 Synopsis of Bash
- 2. Bash as a Shell
- 2.1 How to Run a Bash in Linux
- 2.2 Finding Bash in Linux
- 2.3 How to Make a Bash File in Linux
- Conclusion
- Beginners Guide for Bash Command in Linux
- Tutorial Details
- Syntax of the Bash Command
- Starting an Interactive Shell Session
- Starting an Interactive Shell Session without User Configuration
- Invoked Bash as the Login Shell
- Executing a Specific Command
- Executing a Specific Script
- Executing a Specific Script While Printing Each Command Before Executing It
- Executing a Specific Script and Stopping at the First Error
- Starting Restricted Bash Shell
- Printing Shell Input as It Is Typed
- Invoking Bash POSIX Mode
What Is Bash in Linux?
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.
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. 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.
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:
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.
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.
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.
What is Bash in Linux? [Running, Finding & Making]
Creating own script according to personal demand is the most fascinating thing a programmer wishes for. Bash gives us just that perfect option to create our own scripts in Linux. In this article, you will get to know what is bash in Linux and other interesting options of bash which will make you a pro user.
What is Bash in Linux?
1. Bash as a Shell
Bash(Bourne Again Shell) is the most popular command language interpreter for the GNU operating system. It supports functions, variables, flow controls and also reads and executes commands from a file. It works as the middleman or shell between the computer and the human command line.
2. Bash as a command
Inside the bash shell, there is a bash command also. This command is used to run files or text windows.
1. Bash as a Command
1.1 Synopsis of Bash
On the man page of bash, you can find all the descriptions and uses of the bash command. It takes all the single-character shell options and a number of multi-character options. Bash pipes the command strings in a specified file.
2. Bash as a Shell
2.1 How to Run a Bash in Linux
Whenever you are using a terminal in our Linux operating system, you are using the bash every time. To run a bash shell in your machine you can follow some steps given below: Steps to Follow: ➊ At first open the Ubuntu Terminal by pressing CTRL + ALT + T. ➋Type the following command in the command prompt:
➌Press the ENTER button. Output: After that, you will be able to see that you are running bash as your shell or interpreter. Note: If you don’t see this, it means you are running other shells. You can download and install bash if you want to use it.
2.2 Finding Bash in Linux
You need to know where the bash interpreter is located on your machine. To learn about it you should follow the steps given below: Steps to Follow: ➊ Open the terminal first. ➋ Type the following command in the command prompt:
➌Press the ENTER button. Output: After that, you will be able to see the location of your bash interpreter. As you can see here, my bash shell is located in the /usr/bin/bash directory.
2.3 How to Make a Bash File in Linux
To make a bash file you should follow some procedures. Those are given below: Steps to Follow: ➊ Open the terminal first. ➋ You can choose any text editor to open a file. Here I have used the nano text editor to open a bash file. I have given the following command in the command prompt:
➌ After opening the nano file you have to give the following command first. This is a must to begin any bash file.
Though other lines starting with a # sign do not have any particular meaning. They are just taken as a comment by the machine. Only this line starting with a #! (shebang) sign in the bash file has a particular meaning. So the second line in this file is just a comment, you don’t have to write it. ➍ Next, I have put three values. Those are:
echo “Hello, how are you? $USER” echo “Today is $(date)” echo “Have a good day”
I want to execute these three values. Here $USER gives us the user name of the machine. $(date) will show the date and time of the machine. ➎ Finally you just save the file using CTRL+S and exit the file press CTRL+X. Output: Now you come back to your command prompt. To run the bash file you have to type this command:
You can see in the picture that three echo commands are executed correctly and got the desired values by just running the file. Where borhan is the user of the machine and Mon Nov 28 4:421:38 PM is the current date and time.
Conclusion
Beginners Guide for Bash Command in Linux
The bash (Bourne-Again SHell) is a sh-compatible command-line interpreter that reads from standard input or from a file and gives you the resulting output.
Today, you will learn how to use the bash command in Linux with different options to run commands or scripts.
Tutorial Details
Description | Bash (Bourne Again Shell) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | bash |
Internet Required | No |
Discussed Tools in this Article |
Syntax of the Bash Command
The bash command takes two arguments: one is the option, and the other is the command string or file path.
$ bash [OPTION] [COMMAND STRING/FILE PATH]
Starting an Interactive Shell Session
When you execute the “bash” command with “ -i ” or without any flag in your terminal, it will start a new bash process.
If your default shell is bash, then you will find multiple bash processes running on your system.
$ ps PID TTY TIME CMD 42753 pts/1 00:00:00 bash 87029 pts/1 00:00:00 bash 87035 pts/1 00:00:00 ps
Note that when you start the bash interactive shell session, it will load the “~/.bashrc” configuration file.
Starting an Interactive Shell Session without User Configuration
Use the “ —norc ” flag to run the interactive shell session without reading or executing the startup configuration file (“~/.bashrc“).
Invoked Bash as the Login Shell
The “ -l ” or “ —login ” flag is used to let Bash act as if it had been invoked as a login shell. The login shell reads certain initialization files (also known as interactive login shell files) from your home directory, like “~/.bash_profile“.
Executing a Specific Command
If you use the “ -c ” flag with the bash command, it will read and execute the first non-optional argument and ignore the remaining arguments.
$ bash -c "echo 'Executed by bash'" Executed by bash
Executing a Specific Script
To execute a specific script in Bash, specify its absolute or relative path to the bash command.
$ bash ./script.sh Script Executed
Executing a Specific Script While Printing Each Command Before Executing It
Use the “ -x ” flag to print each command and their arguments in the script as they are executed.
$ bash -x ./script.sh + echo 'Script executed' Script executed
Executing a Specific Script and Stopping at the First Error
By default, when you execute a script with the bash shell, if any error occurs while executing, it will be printed and the rest of the script will continue to execute.
However, if your script has a bunch of things (functions, an array, if-else statements, etc.), a sudden error might interrupt the behavior of your script, and you might want to stop the script from executing further.
Use the “ -e ” flag with the bash command to prevent your script from executing after the first error.
Bash example without flag:
$ bash ./script.sh Script executed ./script.sh: line 5: wrong-command: command not found Error Avoided
$ bash -e ./script.sh Script executed ./script.sh: line 5: wrong-command: command not found
Starting Restricted Bash Shell
The “ -r ” or “ —restricted ” flag will start the bash in restricted mode, which is more controlled than the standard shell. Overall, the behavior will be identical to bash except with a few restrictions.
- Changing directories with the cd command is not allowed.
- Executing shell scripts is not allowed.
- Setting or unsetting the PATH variable is not allowed.
- Importing functions is not allowed.
- Turning off restrictions using “set +r” or “set +o” is not allowed.
- Redirection signs “>“, “>>“, “>|“, “<>“, “>&“, and “&>” are not allowed.
- Specifying file names with arguments like “/” or “–” is not allowed.
$ bash -r $ cd bash: cd: restricted
Printing Shell Input as It Is Typed
The “ -v ” flag will print all the typed commands on screen with the resulting output.
$ bash -v $ whoami whoami linuxtldr
Invoking Bash POSIX Mode
Using the “ —posix ” flag will change the behavior of bash to act more closely to the POSIX standard and avoid the things that differ bash from the POSIX standard.
The following is a list of changes that occur when “POSIX mode” is enabled:
- The POSIX startup file will be executed rather than bash files.
- Bash will set the “ POSIXLY_CORRECT ” variable.
- Bash will search the “$PATH” to find a new location if it is no longer in the hash table.
- Alias expansion is always enabled.
- The default history file is “~/.sh_history“.
$ bash --posix
And here we will end the article.
If you have any suggestions related to this topic, feel free to tell us in the comment section.