A Bash script is a plain text file containing a series of commands. Instead of typing commands one by one, you can run them all at once by executing the script. This is useful for automation, configuration, and simplifying repetitive tasks.
Bash Script Examples
1. Creating a Script
First create the script file using a text editor like nano or vim:
Then add your script content:
Don’t forget to save your file in nano:
- Press
Ctrl + Oto write/save the file - Press
Enterto confirm the filename - Press
Ctrl + Xto exit the editor
2. Making Scripts Executable
Use chmod to make the script executable:
Then run the script:
You can also run scripts using:
This explicitly invokes the bash interpreter to run the script. This method works regardless of the script’s executable permissions, but it’s good practice to make scripts executable.
Important Points
- Bash scripts must be saved with Unix line endings (
LF). - Use
chmod +xto make the script executable. - Use
./scriptname.shto run it like a program.
Try It Out!
Try writing a script that prints your name, lists the current directory, and prints “Done”. Run it directly using ./.
TD Bash PlayCloud
Please click the “Play” button to launch the terminal and wait for it to load. If you’re unable to type, kindly click the refresh button located at the upper right corner.