Basic syntax: What is it really?¶
Bash (or Bourne Again SHell) is a command-line interpreter used on Unix-based systems like GNU/Linux.
A bash script is a text file containing a series of commands that you would normally write in a terminal.
It starts out like this:
The #!/bin/bash is called a shebang. It tells the system to use Bash to interpret the script.
Then, you need to make the script executable. You can do this by running chmod +x path/to/script.sh in your terminal.
Variables¶
- No spaces around the equal sign (
=) when declaring variables. - Use the dollar sign (
$) to access a variable. - Use the
readcommand to read input into a variable.
Declare a variable like this:
Use a variable like this:
Read input into a variable like this:
Conditionals¶
Use the syntax below to perform a conditional check:
You can also compare numbers using the following operators: -eq, -ne, -lt, -le, -gt, and -ge.
Loops¶
Use the for and while loops to iterate over a range of values.
For example:
Or:
Function Declaration and Call¶
Declare a function like this:
Call a function like this:
Or, you can call a function with arguments:
Additional Syntax Rules:¶
- You cannot assign a value to a variable inside a quote.
- Bash does not support partial string assignments.
- You cannot use the equal sign (
=) operator inside anifcondition.