Bash Scripting Course - "Hello, World!" Script
Introduction
- This video covers the basics of Bash scripting, starting with the traditional "Hello, World!" script.
What is Bash?
- Bash is a shell, which is an interface that allows users to interact with the operating system by entering commands.
- When a command is entered into the terminal, the shell interprets the command and presents the results as output.
- Example: The
ls command lists the contents of the current working directory. - Administrators use many commands, and automation through scripting helps avoid repetitive tasks.
Interacting with the Shell
- Typing a command (e.g.,
ls) and pressing Enter executes the command. - Incorrect commands result in an error message.
- The command line is powerful and allows for server management, such as navigating directories (
/etc), listing storage, installing programs, and rebooting the server.
Automation with Scripts
- Scripts automate processes by including commands to be run in sequence.
Different Shells
- Linux and Unix systems offer different shells; Bash is the default on most Linux systems.
- To determine the current shell, use the command
echo $SHELL. - Other shells, like
zsh, are valid but this course focuses on Bash. - If Bash is not the current shell, use
which bash to find the Bash interpreter's location. - Run the Bash interpreter by typing its location in the command line (e.g.,
/usr/bin/bash). - Confirm the shell by running
echo $SHELL again to ensure it shows Bash.
What is a Bash Script?
- A script is a text file containing one or more commands.
- When the script is executed, the commands in the file are run as if they were entered manually.
- Example: Creating a script named
myscript.sh with the ls command inside.
Creating and Executing a Simple Script
- Use a text editor (e.g.,
nano) to create a new file, such as myscript.sh. - Type the
ls command into the file and save it. - Mark the script as executable using the command
sudo chmod +x myscript.sh. - Executing the script: Type
./myscript.sh in the terminal. - The script will run the
ls command, displaying the contents of the current working directory.
Scripting Considerations
- Scripts are valuable when they save time by automating tasks.
- Naming scripts with the
.sh extension is a common practice but not required. - The name of the script file does not affect its functionality as long as it contains valid commands and is marked as executable.
Adding Multiple Commands to a Script
- Open the script again using a text editor (e.g.,
nano). - Add a new command on a new line, such as
pwd (print working directory). - When the script is executed, the commands will run sequentially, starting from the top.
Proper Bash Script Structure: The Shebang
- A shebang (
#!/bin/bash) is the first line of a Bash script. - It tells the system which interpreter to use to run the script.
- Even if the current shell is not Bash, the script will execute using Bash.
- The shebang is essential to ensure the script is interpreted correctly.
"Hello, World!" Script Example
- Create a script with the shebang at the top:
#!/bin/bash. - Add the command `echo