Shell Scripting: Linux Command Line Automation Guide
The clearest way to speed up repetitive tasks in the Linux terminal is to use shell scripts.
In this guide, you will write a script from scratch, run it and make it interactive.
What Will You Learn in This Guide?
- What is shell script and why is it used?
.shfile creation and shebang logic- Script execution and permission management
- Variables and receiving input from the user
- Practical tips for error checking
What is Shell Script?
Shell script is an automation file in which Linux commands are written sequentially.
The aim is to run manual jobs with a single command.
1. Getting Started with Shebang
Each shell script must specify which interpreter to use.
This line is called shebang.
#!/bin/bash
- This line tells the script to run in Bash.
Alternatively:
#!/bin/sh
- This line uses the generic shell interpreter.
2. Creating the First Shell Script File
- Create a new file with a text editor.
vi ilk_betik.sh
- This command opens a new script file.
Add the following to the file:
#!/bin/bash
- Shows active user and date
date
- This script prints the username and system date to the screen.
3. Script Execution Methods
- You can run the script in two ways.
- Running with Bash
bash ilk_betik.sh
- This method does not require additional permissions.
- Direct operation
./ilk_betik.sh
- This method requires execution permission.
4. Grant Run Permission (chmod)
- Add execute permission to the script.
chmod +x ilk_betik.sh
- This command makes the file executable.
5. Variable Usage
- In shell scripts, variables are defined without spaces.
#!/bin/bash
MESAJ="Merhaba Linux"
echo $MESAJ
- This script prints the variable contents to the screen.
6. Getting Input from the User
- It is possible to make scripts interactive.
#!/bin/bash
echo "Adınız nedir?"
read ISIM
echo "Merhaba $ISIM, hoş geldin"
- This script takes the name from the user and generates a response.
7. Stopping the Script in Case of Error
- When the command gives an error, you can ask the script to stop.
#!/bin/bash
set -e
- This line stops the script when an error occurs.
Frequently Asked Questions (FAQ)
1. Why does the script give a Permission denied error? There is no execution permission in the file. It is solved with chmod +x.
2. Why doesn't the script written in Windows work in Linux? Line endings are different. It must be converted with dos2unix.
3. Should I use sh instead of bash? For simple tasks, sh is sufficient. Bash is recommended for advanced scripts.
Result
Shell scripts are the basis of automation in Linux systems. You can start with small scripts and build powerful workflows.
You can immediately run the scripts you create on high-performance servers on GenixNode infrastructure 🚀

