Introduction
For many people, the idea of creating a bash script may seem daunting. After all, just the thought of typing out complicated commands can put off even the most tech-savvy users. But the truth is, bash scripting doesn’t have to be complicated. With the right approach and knowledge, you can easily write bash scripts with #!/bin/bash
and unlock the power of scripting. In this blog, we will explore how you can write a bash script with #!/bin/bash
, and look at five tips and examples from itvraag.nl to help you increase your productivity.
What is Bash Scripting?
Bash scripting is a type of scripting language based on the Linux shell. Bash (which stands for “Bourne-again shell”) allows users to write scripts that can be used to automate certain tasks, such as searching for files and running commands. To write a script in bash, you need to use the #!/bin/bash
command at the beginning of the script.
Writing a Bash Script
When writing a bash script, it is important to remember that each command must be on a separate line. To write a bash script, open a text editor and type the following command at the top of the file: #!/bin/bash
. After that, you can start writing your code. To test the code, save the file and type “./filename” in the terminal.
Examples from itvraag.nl
Here are five examples from itvraag.nl that can help you write and run a bash script with #!/bin/bash:
$ grep -rl 'text to search' /path/to/search
– grep command will search for a specific text string in the specified directory.$ grep -r 'text to search' /path/to/search | wc -l
– wc command will count the number of lines containing the text string.$ sed -i 's/text to search/replacement text/g' /path/to/file
– sed command will replace all occurrences of the text string with the replacement text.$ awk '/text to search/ {print $2}' /path/to/file
– awk command will print the second column of the text string.$ for file in /path/to/dir/*; do mv "$file" "$(echo $file | sed 's/text to search/replacement text/')"; done
– for loop command will replace the text string in each file in the specified directory with the replacement text.
Surprising fact: Bash scripts can also be used to write programs that can be used to automate system administration tasks.
How to Make a File Executable
To run the bash script you’ll need to make it executable. All you need to do is assign the file with execute permissions. For example, if you want to make the file script.sh
executable, you can use the following command:
$ chmod +x script.sh
The above command will make the file executable for the user who owns it. If you want to make it executable for everyone on the system, you can use the following command:
$ chmod 755 script.sh
Once the file is executable, you can run it by simply typing ./script.sh
.
Running a script
To run a Bash script, open a terminal window and type the path to the script file, then press Enter. For example, if the script file is located in your home directory, you can run it by typing:
~/script.sh
If the script is located in a different directory, you will need to provide the full path to the script file. For example:
/path/to/script.sh
Alternatively, you can change to the directory where the script is located and run it by simply typing its name:
cd /path/to/script
./script.sh
If the script is in a directory that is listed in your PATH
environment variable, you can run it by simply typing its name, regardless of your current directory.
It’s also possible to make a Bash script executable by changing its permissions and adding a shebang at the top of the script file. To do this, open the script file in a text editor and add the following line at the top:
#!/usr/bin/env bash
This specifies that the script should be interpreted by the bash
shell. Save the file and then set the executable permission bit with the chmod
command:
chmod +x script.sh
You can then run the script by typing its name, just like any other executable:
./script.sh
It’s also possible to specify the interpreter directly by including the path to the interpreter at the top of the script, like this:
#!/bin/bash
This specifies that the script should be interpreted by the bash
shell located at /bin/bash
.
Key Takeaways
To summarize, bash scripting can be a powerful tool for automating tasks. With the right knowledge and approach, it is possible to write a bash script with #!/bin/bash and unlock the power of scripting. Here are five tips and examples from itvraag.nl that can help you increase your productivity:
- Use the
$ grep -rl 'text to search' /path/to/search
command to search for a specific text string in the specified directory. - Use the
$ grep -r 'text to search' /path/to/search | wc -l
command to count the number of lines containing the text string. - Use the
$ sed -i 's/text to search/replacement text/g' /path/to/file
command to replace all occurrences of the text string with the replacement text. - Use the
$ awk '/text to search/ {print $2}' /path/to/file
command to print the second column of the text string. - Use the
$ for file in /path/to/dir/*; do mv "$file" "$(echo $file | sed 's/text to search/replacement text/')"; done
command to replace the text string in each file in the specified directory with the replacement text.
Challenge
Now that you know how to write a bash script with #!/bin/bash, why not test your knowledge by writing a script that automates a system administration task?