Ends in
00
days
00
hrs
00
mins
00
secs
SHOP NOW

🎊 Black Friday is here! Enjoy up to 30% OFF ALL AWS Associate-Level Courses!

Back to Course

Bash Scripting for Beginners with Hands-On Labs

0% Complete
0/0 Steps

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:

Bash Scripting Syntax Example
bash
nano myscript.sh

Then add your script content:

Bash Scripting Syntax Example
bash
#!/bin/bash
echo "Script started"
ls
echo "Script ended"

Don’t forget to save your file in nano:

  • Press Ctrl + O to write/save the file
  • Press Enter to confirm the filename
  • Press Ctrl + X to exit the editor

2. Making Scripts Executable

Use chmod to make the script executable:

Bash Scripting Syntax Example
bash
chmod +x myscript.sh

Then run the script:

Bash Scripting Syntax Example
bash
./myscript.sh

You can also run scripts using:

Bash Scripting Syntax Example
bash
bash myscript.sh

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 +x to make the script executable.
  • Use ./scriptname.sh to 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 ./.

Terminal Playground Embed Test

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.

Skip to content