
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Run Bash Commands from a File
Bash script is a scripting language like other languages that have an extension and a way to interpret it. To interact with the operating system through the command line, commands are used.
There are a lot of commands on Linux, and new commands are added regularly. However, all these commands have a common way of working. All commands consist of three parts: the command itself, its parameters, and arguments.
Bash Script File
A bash script file is a file that contains a sequence of commands put together to do a certain task. If you have a lot of commands, it becomes hard to put them in the command line. We need a file that will organize them in a certain way, but the output is the same whether you run a command from a file or directly in the terminal?the result will be the same.
For example, take one of the most common commands, ls, which is designed to display a list of files and directories in a specific folder. With the simple input ls, we will get a list of files and directories from the current folder. Whether you run it from a file or from the terminal directly, the output will be a list of files and directories located in the current directory.
The bash file should have an extension at the end, after the name of the file, .sh, indicating that this file is a bash script. For example: hello.sh.
How to Run a Bash Script File?
To execute a bash script file, there are multiple ways to do that. The common way is creating the file, giving it permission, and executing it.
For example, if I have a file named hello.sh, the first thing we do is give the file permission to run, because by default, when we create a regular file, it doesn't have permission to execute in the system. We first need to assign it execution permission using the command ?
sudo chmod +x hello.sh
We use the command chmod, which stands for "change mode." This command is used to assign permission to a file. The option +x tells it to give execution permission to the file hello.sh.
After we assign permission, we can run the file using ./ before the file name, like this ?
./hello.sh
Run a Command from a Text File
We said that a bash script file needs to have .sh at the end, but we do have an exception here: we can run a bash command from a regular text file.
Let's take an example. I created a text file called hello.txt. The .txt at the end indicates that this file is a text file. Inside the file, we put the following code ?
echo "Hello from a text file"
This file has a command, echo, that will print the message "Hello from a text file" to the screen.
The question is how can we use bash to execute the file? In fact, we have many ways.
Using the bash Command
Just like with an ordinary bash file, we can use the bash command to run a text file directly in the terminal like this:
bash hello.txt
The output of this should be ?
Hello from a text file
As you see, even though the file is a text file, using the bash command allows us to run it as a bash file.
Using Source to Execute a Text File
Just like the bash command, we can use source to execute a text file in the command line ?
Hello from a text file
Difference between bash and source Commands
If you see the example we made, you may think there is no difference between the bash command and source. But behind the scenes, there is a difference.
The bash command runs the script in a new Bash shell. This means when we run our file, the bash command runs it in a separate shell environment. This means any changes our script makes will not affect the current shell because they are set in the subshell (new shell created by Bash).
To see this, let's take this example and change our hello file to this code ?
export name="Hello from a text file" echo $name
This file sets a variable called name and prints it using echo. If we run this, we should get ?
However, if we try to print the variable name in the current shell after running the script, we will find it empty, because the variable was set in the subshell and does not affect the current shell. Once the script finishes, the subshell is closed.
Let's do the same example, this time using source ?
As we can see, when we print the variable name, we get the value because the source command sets the variable in the current shell environment.
Bash or Source?
You may be confused about whether to use bash or source to run a command, but if you pay attention to what we explained, you may know which one to use.
- If you want to run a script that is isolated and doesn't make any changes to environment variables, the best way to do that is by using the bash command.
- If you want to run a script that modifies the current shell environment (for example, applying some changes to the ~/.bashrc file), then the best way is by using the source command.
Conclusion
In this tutorial, we explored different ways to run a bash script file. First, we explained how to run a regular bash file, then we did an example with a text file, and lastly, we clarified some key differences between bash and source.