Shell Scripting in Linux: Running .sh, Bash and Python Scripts
A shell in Linux is a command-line interpreter that sits between the user and the operating system kernel. It reads commands you type, passes them to the kernel for execution, and returns the output to your screen. Bash (Bourne Again SHell) is the default shell on most Linux distributions. Shell scripts are plain text files that chain commands together to automate repetitive tasks.
- Key Takeaway 1: The shell in Linux translates your commands into instructions the Linux kernel can execute.
- Key Takeaway 2: Bash is the most common shell;
shis its older, POSIX-compliant ancestor. - Key Takeaway 3: A shebang line (
#!/bin/bash) tells Linux which interpreter to use when running a script. - Key Takeaway 4:
chmod +xmakes a script executable; you can also run it directly withbash script.shwithout changing permissions. - Key Takeaway 5: Scripting is the first real step toward DevOps automation, and employers notice it.
What Is a Shell in Linux, and Why Does It Matter?
When you open a terminal on Ubuntu, Fedora, or even a Kali Linux session, you are dropped into a shell. Most of the time that is Bash, which has been the default shell in Linux systems since the late 1980s. The shell reads your input from stdin (standard input), processes it, and writes results to stdout (standard output). That pipeline is the foundation of every automation script you will ever write.
There is a common point of confusion between sh and bash. The sh command runs the system’s POSIX-compliant shell, which on many modern distros is actually Dash, not Bash. Bash supports arrays, double brackets for conditionals, and string manipulation features that sh does not. If your script uses Bash-specific syntax, always specify #!/bin/bash at the top, not #!/bin/sh.
According to the Stack Overflow Developer Survey 2024, Bash/Shell is used by 34.4% of professional developers, making it the fifth most-used language overall. For anyone building a career in cloud infrastructure, DevOps, or cybersecurity, that number is a signal worth paying attention to.
sh vs Bash: A Quick Comparison
| Feature | sh (POSIX) | Bash |
|---|---|---|
| Arrays | Not supported | Supported |
Double brackets [[ ]] |
Not supported | Supported |
| String manipulation | Limited | Extensive |
| Portability | High (runs anywhere) | Requires Bash installed |
| Default on Ubuntu | No (Dash runs as sh) | Yes (interactive shell) |
| Recommended for beginners | No | Yes |
How to Write and Run Your First Shell Script in Linux
Let’s build one script and take it all the way from blank file to running output. Call it hello.sh. This single project thread covers every practical skill you need right now.
Step 1: Create the File with nano or vi/vim
Open your terminal and type nano hello.sh. Nano is beginner-friendly because the controls are printed at the bottom of the screen. Type your script, then press Ctrl+O to write (save) and Ctrl+X to exit.
If you want to use vi or vim instead, type vi hello.sh. The vi editor in Linux has two modes: command mode and insert mode. Press i to enter insert mode and start typing. To save and quit, press Esc, then type :wq and hit Enter. To quit without saving, use :q!.
Vim is the improved version of vi, pre-installed on most systems. It adds syntax highlighting and a more forgiving undo system. For most beginners in India learning Linux through platforms like NPTEL or college lab sessions, nano is the faster start, but knowing vim is a career asset since many servers only have vi available.
Step 2: Write the Script with a Shebang
Your hello.sh file should look like this:
#!/bin/bash
echo "Hello, automation!"
The first line is the shebang. It tells the kernel to use /bin/bash to interpret the rest of the file. Without it, the system might guess the wrong interpreter and your script will fail in confusing ways.
Step 3: Make It Executable with chmod +x
By default, a new file is not executable. Run chmod +x hello.sh to grant execute permission. Now you can run it two ways:
- ./hello.sh — uses the shebang to pick the interpreter automatically.
- bash hello.sh — calls Bash explicitly; works even without
chmod +x.
If you are wondering how to run a .sh file in Linux on a remote server where you do not have sudo access, bash script.sh is your safest fallback. No permission change needed.
Learning to write and deploy scripts like this is exactly the kind of hands-on skill that distinguishes a junior sysadmin from someone ready for a DevOps engineering role. Automation starts here.
Step 4: Extend the Script
Add a variable and a loop to your script. Change hello.sh to include:
#!/bin/bash
NAME="Linux Learner"
for i in 1 2 3; do echo "Run $i: Hello, $NAME!"; done
Run it again with ./hello.sh. You will see three lines of output. That loop is the beginning of real automation: batch file processing, log parsing, deployment checks. All of it scales from this pattern.
How to Exit Vim (and Not Panic)
This deserves its own section because it is genuinely one of the most searched Linux questions on the planet. According to Stack Overflow, the question “How do I exit Vim?” has been viewed over 4.7 million times. You are not alone.
Here is the complete exit cheat sheet for vim:
- :wq — save (write) and quit.
- :q! — quit without saving, force exit.
- 😡 — save and quit (only writes if changes were made).
- ZZ — shortcut for
:x, works in command mode. - Esc then :q — quit if no changes were made.
The key thing to remember: always press Esc first to make sure you are in command mode, then type your colon command. If you see characters appearing in your file instead of a command prompt at the bottom, you are still in insert mode. Hit Esc again.
If you want to get comfortable with basic Linux terminal commands before scripting, the Kali Linux beginner commands guide at 3.0 University is a solid starting point.
How to Run a Python Script in Linux
Once you are comfortable with the shell in Linux and Bash scripting, Python becomes a natural next step. It is more readable for complex logic, has a massive standard library, and integrates cleanly with Linux system calls.
To run a Python script, create a file called hello.py with this content:
#!/usr/bin/env python3
print("Hello from Python!")
Then run it one of two ways:
python3 hello.py— explicit interpreter call, works without execute permission.chmod +x hello.pythen./hello.py— uses the shebang, treats it like a shell script.
The shebang #!/usr/bin/env python3 is preferred over hardcoding /usr/bin/python3 because it finds whichever Python 3 is active in your environment, which matters when you are working with virtual environments or pyenv.
According to the TIOBE Index (June 2025), Python is the number one programming language globally. According to the NASSCOM Future of Work Report 2023, Python is the top language skill demanded by tech employers in India. Combining Python with shell scripting in Linux gives you a serious productivity edge in the Indian IT job market.
If you want to sharpen your Python knowledge further, the Python interview questions guide at 3.0 University covers the concepts employers actually test.
Bash Script vs Python Script: When to Use Which
| Use Case | Bash Script | Python Script |
|---|---|---|
| File system operations | Excellent | Good |
| Chaining Linux commands | Excellent | Awkward |
| Complex data processing | Painful | Excellent |
| API calls and HTTP | Possible (curl) | Excellent (requests) |
| Startup scripts / cron jobs | Excellent | Good |
| Readability for teams | Low-Medium | High |
The real-world answer is: use Bash for gluing Linux commands together, use Python when your logic gets complex. Most DevOps engineers use both every day.
Understanding what a shell in Linux does, and how to script with it, is the gateway skill. Once you can write, permission, and execute scripts confidently, you can start building CI/CD pipelines, automated deployment scripts, and infrastructure-as-code workflows. That is the practical path into DevOps, and it starts with ./hello.sh.
Ready to go further? Explore 3.0 University’s programming and DevOps learning paths at 3university.io and start building the skills that tech teams in India and globally are actively hiring for.
Frequently Asked Questions
What is a shell in Linux?
A shell in Linux is a command-line program that interprets your commands and passes them to the operating system kernel for execution. Bash is the most widely used shell on Linux systems. It reads from stdin, processes commands, and returns output to stdout. Shell scripts are text files containing sequences of these commands, run automatically by the interpreter.
How do you run a .sh file in Linux?
You have two main options. First, make it executable with chmod +x filename.sh, then run it with ./filename.sh. Second, call Bash directly with bash filename.sh, which does not require execute permissions. The second method is useful on shared servers where you do not have permission to change file modes.
How do you execute a shell script in Linux?
To execute a shell script in Linux, add a shebang line (#!/bin/bash) as the first line of your file, save it with a .sh extension, then run chmod +x script.sh to set execute permission. Call it with ./script.sh from the same directory, or use the full file path from anywhere on the system.
How do you exit Vim?
Press Esc to make sure you are in command mode, then type :wq and press Enter to save and quit. Use :q! to quit without saving. If you have made no changes, :q alone works. The ZZ shortcut (capital Z twice) also saves and exits. Always start with Esc if you are unsure which mode you are in.
How do you run a Python script in Linux?
Create a .py file with #!/usr/bin/env python3 as the first line. Run it with python3 script.py for the simplest approach. Alternatively, run chmod +x script.py and then ./script.py to execute it directly using the shebang. Make sure Python 3 is installed on your system first by running python3 --version in your terminal.
Last updated: June 2025. Reviewed by the 3University editorial team.


