What is Bash in Linux? What can it be used for?
Writing Bash scripts is one of the best ways to increase your Linux expertise, but learning it can be intimidating. Here's what you need to know about Bash and how you can get started with basic scripting.
What is Bash in Linux?
Bash (Bourne-Again SHell) is the default command line interface (CLI) you will use in most Linux distributions. It's the interpreter used in Terminal. You can use it to install applications, run command line programs, and add new functionality via scripting.
Shell scripts allow you to do things in Bash that can be done with other basic programming languages, all with convenient access to your operating system and files. Commands in Terminal are usually very simple scripts. You can take Terminal usage to the next level by writing your own Bash scripts.
Bash and other CLI shells are more difficult to use than graphical user interfaces (GUIs), but they have several advantages:
- They are less resource intensive because they do not display graphics.
- When you have scripting skills, they will perform tasks like listing folder names and all subfolders much faster.
- They can be used to calculate information (like the number of files whose names start with 'a') that are not supported through GUI applications.
- Sorting data in files (like CSV) is more efficient with CLI scripts.
What can Bash be used for?
Bash scripts are useful for automating basic tasks. You'll have a slow start when you first start writing scripts, but as you grow more confident and knowledgeable, you'll find ways to complete almost any complex task with just a few minutes of focused scripting.
An easy way to get started using Bash is to use cron, a Linux service that schedules tasks. You can use it to run specific scripts or other tasks at a specific time or interval. cron uses a text file (crontab or 'cron table') to keep track of which commands are scheduled and when.
For example, if you want to make your Ubuntu system automatically update its software and package repositories once a week, a well-written cron task can be useful. You can start performing this task by opening Terminal, typing nano to open a text editor, and writing the following script into a file named autoupdate.sh :
#!/bin/bash sudo apt-get update
After writing, saving and closing that script (ideally saving it in the home directory), the next step is to add it to the crontab. You can open crontab for editing by entering the following command in Terminal:
crontab -e
Once the crontab is open, the next step will be to add a task. To have autoupdate.sh run once per week, you need to write something like this before saving and exiting the file:
* * * * 0 ./autoupdate.sh
You can even write cron tasks to clean up files or use the at command to schedule non-recurring tasks through the Bash shell. By combining both of these utilities, you can automate most of the repetitive or predictable tasks that need to be done on your computer. Scripts can be created to automatically:
- Update specific computers or applications.
- Send email.
- Change the volume, screen brightness, or other settings.
- Copy, read, edit or manipulate files.
The best way to learn how to write Bash scripts is to follow examples. There are actually countless ways to improve your system using Bash. Following instructions and reading about syntax will gradually improve your scripting abilities. If you want to learn more about Bash, consider trying:
- Check email with mutt.
- Browsing the web using w3m.
- Play music using mpg321.
- Edit images with imagemagick.
- Read and write with vim.
- Browse files with ranger.
- Save files to clipboard using xclip.
- Edit videos with ffmpeg.
How to write scripts with Bash in Linux
If you lack ideas, you can also learn Bash by playing online games. Some of the best games you can play to learn how to write Bash scripts include:
- , a text adventure game hosted on MIT's website.
- , which hosts more than a dozen "war games" to help you learn various scripting skills through problem solving.
How to run Bash script in Linux
You can create and run Bash scripts in Linux using your favorite text editor. vim and nano are both great options, and you can open them by typing their names into Terminal. You can save and name your file immediately, making sure that the file name ends with .sh (shell script file extension).
After writing the script, it can be run using the following Terminal commands:
sudo chmod +x filename.sh ./filename.sh
You may need to start small with Bash scripts. However, with enough practice, you can turn complex, unwieldy, repetitive tasks into small challenges that can be solved in a few minutes. It's an essential skill for tech professionals, power users, and the best place to learn how to use Terminal.
You should read it
- How to create and open folders in VS Code with Git Bash for Windows
- Difference between Zsh and Bash
- What is 'Bash' in Linux?
- Ubuntu Bash tutorial on Windows 10
- Shortcuts to copy - paste coming soon on Windows 10's Bash Console
- Everything you can do with the new Windows 10 Bash Shell
- How to Install and Run Bash on Windows 11
- How to simplify 7z file compression with Bash Alias
- 5 example bash scripts to help you learn Linux programming
- Instructions for accessing Ubuntu Bash files on Windows
- How to change user accounts in Ubuntu Bash Shell Windows 10
- How to use Zsh (or other Shells) on Windows 10?