100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Bash

Writing Your First Bash Script

Get hands-on with the anatomy of a Bash script — shebang, permissions, execution, comments, and structuring commands into a reusable, repeatable tool.

Bash Scripting FundamentalsBeginner8 min readJul 9, 2026
Analogies

Writing Your First Bash Script

A Bash script is simply a text file containing a sequence of shell commands that Bash executes in order, exactly as if you had typed them interactively. Scripting turns repetitive manual work — backups, deployments, log rotation, environment setup — into a single reliable, version-controllable command. The barrier to writing your first script is low: you need a text file, a shebang line telling the kernel which interpreter to use, executable permission, and a location the shell can find. From there, everything you already know about running commands at the prompt applies inside the script.

🏏

Cricket analogy: A training script is simply a written sequence of drills a coach runs through in order, exactly as if calling them out live; turning repetitive nets sessions into one documented routine needs a written plan, a header naming the coach in charge, sign-off to run it, and a spot in the team's training folder.

The shebang and how scripts are executed

The first line of a script, #!/bin/bash (or the more portable #!/usr/bin/env bash), is called the shebang. The kernel reads these first two bytes (#!) and uses the rest of the line to determine which interpreter should run the file — this is how ./script.sh knows to invoke Bash rather than trying to execute the text as a binary. #!/usr/bin/env bash is preferred in portable scripts because it locates bash via the user's PATH rather than assuming it lives at /bin/bash, which matters on systems (like some macOS setups or containers) where Bash is installed elsewhere. If you omit the shebang and instead run the script as bash script.sh, the shebang line is ignored and treated as a comment.

🏏

Cricket analogy: The team sheet's header line names the format being played (Test vs T20), and officials read just that header to know which rulebook applies before the toss; naming the format generically as 'limited overs' (like #!/usr/bin/env bash) is more portable across grounds than hardcoding one ground's rules, and if you skip the header, the format assumption is ignored entirely.

bash
#!/usr/bin/env bash
#
# backup-home.sh — archive the current user's home directory
# Usage: ./backup-home.sh [destination-dir]

set -euo pipefail   # exit on error, unset variable, or failed pipeline stage

dest="${1:-/var/backups}"
timestamp="$(date +%Y%m%d-%H%M%S)"
archive="${dest}/home-backup-${timestamp}.tar.gz"

mkdir -p "$dest"
tar -czf "$archive" -C "$HOME" .

echo "Backup written to $archive"
ls -lh "$archive"

Making a script executable and running it

Newly created scripts are not executable by default; chmod +x script.sh adds the executable bit for the owner (and typically group/other depending on umask). Once executable, ./script.sh runs it from the current directory — the leading ./ is required unless the script's directory is in your PATH, because Bash does not search the current directory for commands by default (a deliberate security measure). Alternatively, bash script.sh or sh script.sh runs it by explicitly invoking an interpreter, bypassing both the shebang and the need for the executable bit, though sh may interpret Bash-specific syntax differently.

🏏

Cricket analogy: A newly registered player isn't automatically cleared to play (chmod +x) — the board must certify them first; even then, you must name them explicitly on the team sheet (./script.sh) rather than assume selectors will notice by default, since committees don't scan the crowd for unlisted talent; alternatively, a wildcard entry (bash script.sh) bypasses certification but may be interpreted differently under a different format's rules.

bash
chmod +x backup-home.sh
./backup-home.sh /mnt/backups

# Or run without the executable bit at all
bash backup-home.sh /mnt/backups

# Install it on PATH so it can be called by name anywhere
sudo cp backup-home.sh /usr/local/bin/backup-home
sudo chmod +x /usr/local/bin/backup-home
backup-home

set -euo pipefail is often called Bash's 'strict mode.' -e exits immediately if any command returns non-zero, -u treats referencing an unset variable as an error rather than silently substituting an empty string, and -o pipefail makes a pipeline fail if any stage fails, not just the last one. Together they catch classes of bugs — typoed variable names, silently swallowed errors — that make ad-hoc scripts unreliable in production.

set -e does not catch every failure: it is bypassed inside if conditions, inside &&/|| chains, and for commands whose exit status is checked directly. A common surprise is that a failing command inside a pipeline still lets the script continue unless pipefail is also set, since -e alone only inspects the pipeline's overall (last-command) exit status.

Structure, comments, and style

Good scripts start with a comment block describing purpose and usage, use set -euo pipefail near the top, quote variables, and prefer functions for anything reused more than once. Comments begin with # and run to the end of the line; there is no native multi-line comment syntax, though a : <<'END' here-document is sometimes used as a workaround. Keeping scripts under version control (even a small personal Git repo of ~/.local/bin scripts) is standard practice once you have more than a handful.

🏏

Cricket analogy: A well-prepared team keeps a written game plan header explaining strategy, sets a 'stop immediately on any collapse' rule (set -euo pipefail) rather than playing on through disaster, names players precisely to avoid mix-ups, and reuses a standard fielding drill (a function) rather than re-explaining it each time; notes use a quick marginal comment (#), and the game plan is archived season over season like a version-controlled playbook.

  • A script's first line, the shebang (#!/usr/bin/env bash), tells the kernel which interpreter to execute it with.
  • chmod +x grants execute permission; ./script.sh runs it from the current directory since PATH excludes . by default.
  • set -euo pipefail is standard 'strict mode' that fails fast on errors, unset variables, and failed pipeline stages.
  • Running a script as bash script.sh bypasses the shebang and the need for the executable bit.
  • Comments start with #; there is no built-in multi-line comment, though a here-doc trick can approximate one.
  • Scripts placed on your PATH (e.g. /usr/local/bin) can be invoked by name from anywhere on the system.

Practice what you learned

Was this page helpful?

Topics covered

#Bash#LinuxShellScriptingStudyNotes#DevOps#WritingYourFirstBashScript#Writing#Script#Shebang#Scripts#StudyNotes#SkillVeris

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse