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

🎆 New Year Sale Extension - 25% OFF on ALL Reviewers to Start Your 2026 Strong with our New Year, New Skills Sale!

Back to Course

Bash Scripting for Beginners with Hands-On Labs

0% Complete
0/0 Steps
Lesson 1 of 10
In Progress

Bash Syntax

Bash scripts follow a simple and readable syntax that closely resembles the Linux command line. However, scripting allows you to organize commands into a file and add logic like loops, conditions, and functions.

A script typically starts with the shebang line to specify the shell, and uses common Bash syntax rules for variables, commands, and comments.

Bash Syntax Examples

1. Start your script with a shebang:

Bash Scripting Syntax Example
bash
#!/bin/bash

This tells the system to use the Bash shell to interpret the script.

2. Use the echo command to display a message:

Bash Scripting Syntax Example
bash
echo "This is a Bash script"

3. Add comments using #:

Bash Scripting Syntax Example
bash
# This is a comment
echo "Script is running..."

4. Use semicolons to write multiple commands on one line:

Bash Scripting Syntax Example
bash
echo "Step 1"; echo "Step 2"

Important Points

  • Every Bash script starts with #!/bin/bash.
  • Use # to add comments and document your code.
  • Use semicolons or line breaks to separate commands.

Try It Out!

Try creating a file called syntax.sh, write a basic script that prints two lines and includes a comment.

1. Create the file:

Bash Scripting Syntax Example
bash
touch syntax.sh 

2. Open it for editing:

Bash Scripting Syntax Example
bash
nano syntax.sh 

3. Type your script inside. For example:

Bash Scripting Syntax Example
bash
#!/bin/bash
# This is a simple script
echo "Line 1"
echo "Line 2" 

4. Save and exit nano:

  • Press Ctrl + O, then Enter to save
  • Press Ctrl + X to exit

5. Run the script:

Bash Scripting Syntax Example
bash
bash syntax.sh
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