HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Introduction to Bash

Introduction to Bash

First step into Bash, the language of the command line: printing with echo and variables.

Simple enough for a 5-year-old to understand

Print with echo

Bash scripts consist of commands. echo is used to print to the screen.

First script's
1
echo "Hello!"
Prints text to the screen.
Output / InputPrints text to the screen.
Output / Input
💡 Hint: Double quotes expand variables; single quotes leave them as-is.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.Are there spaces around = in variable assignment in Bash?
2.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

Bash is the most widely used shell on Linux and macOS systems. The shell is the program that takes the commands you write and passes them to the operating system; Bash is both an interactive command line and a programming language.

The purpose of writing Bash scripts is to eliminate repetition. When you write a ten-step manual task into a file, that task runs with a single command and in the exact same way every time. That's why most backup, deployment, and installation tasks are Bash scripts.

Bash differs from other programming languages in one crucial way: every line is a COMMAND. That's why spaces matter, quote usage is critical, and a "successful" value is determined by a zero exit code. These rules can be surprising at first.

What you will learn

  • Creating and running a script file
  • What the shebang line does
  • Granting execution permission
  • Printing to the screen with echo
  • Writing comment lines

Who is this for?

  • Those who manage Linux servers or are learning to manage them
  • Developers who want to automate repetitive tasks
  • Those who will write deployment and installation scripts

When to use?

Bash is the right tool for file operations, process management, chaining system commands, and installation steps: no other language can be this concise for these tasks.

Bash is not suitable for tasks requiring data structures, complex logic, or error handling; when a script exceeds a hundred lines, switching to Python is usually the right decision.

Common mistakes

Wrong

echo "Hello"
./script.sh

Since execution permission was not granted to the file, the shell gives a "Permission denied" error. Creating a script does not make it executable; you must grant the permission separately. Alternatively, you can pass the file as an argument to the interpreter, in which case permission is not needed.

Correct

chmod +x script.sh
./script.sh

Wrong

#!/bin/sh
arr=(1 2 3)
echo "${arr[0]}"

Since the shebang line points to sh, the script runs with the POSIX shell and arrays are not supported: a "Syntax error: ( unexpected" error is thrown. Arrays, double brackets, and many other features are specific to Bash; the shebang line must be bash.

Correct

#!/bin/bash
arr=(1 2 3)
echo "${arr[0]}"

Wrong

#!/bin/bash
echo "Hello"

If a script written on Windows has CRLF line endings, Linux throws a "bad interpreter: /bin/bash^M: no such file or directory" error. Even though the shebang line looks correct, the invisible character at the end is considered part of the interpreter name. You need to convert the file to LF line endings.

Correct

dos2unix script.sh
./script.sh

Frequently asked questions

Is the shebang line mandatory?

If you are running the script directly, yes; without it, the system cannot know which interpreter to use and defaults to the current shell. It is not needed when you pass the file as an argument to the bash command, but writing it in every script is a good habit.

What is the difference between sh and bash?

sh is the minimal shell of the POSIX standard; bash is a superset of it and offers additional features like arrays, double brackets, and string operations. If you are using Bash features, it is mandatory to write bash in the shebang line.

Which one should I use between echo and printf?

echo is sufficient for simple outputs. printf performs formatting and behaves more consistently across systems — especially regarding escape sequences, where echo's behavior varies depending on the shell. If portability is important, printf is preferred.

Summary

Execution permission is required for the script to run.

The shebang line specifies which interpreter to use.

Bash features do not work with sh; the shebang must be bash.

CRLF line endings produce an interpreter error on Linux.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code