linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Processes & resources/shebang

kb/processes ── Processes & resources ── beginner

Shebang: the first line of a script

A script's first line like `#!/usr/bin/env bash` tells the kernel which interpreter to start. Without a shebang the script runs under the current shell, and a bash-only script breaks on /bin/sh in production.

view as markdownaka: hashbang, interpreter-line

What it is

When the kernel gets a request to execute a file that has the +x bit, it reads the first two bytes. If they are #!, called the shebang (or hashbang), the rest of the line is read as a path to the interpreter.

bash
#!/usr/bin/env bash
echo hello

When you run ./script.sh, the kernel sees #! → starts /usr/bin/env bash ./script.sh → env finds bash in $PATH → bash interprets the script.

Without #!, the behavior depends on who runs it:

  • From a bash shell, bash runs it as a bash script
  • From dash/zsh/fish, it may run under the rules of that shell
  • Through exec() with no shell, the kernel refuses with ENOEXEC

So always write a shebang.

/bin/bash vs /usr/bin/env bash

Two forms you see often:

bash
#!/bin/bash             # hardcoded path
#!/usr/bin/env bash     # look up bash through PATH

Prefer the second one. Here is why:

Case/bin/bash/usr/bin/env bash
macOS, bash 5 via brew in /usr/local/bin/bashold 3.xfresh 5.x
Alpine Linux, bash in /usr/bin/bash or absentdoes not workworks if installed
NixOS, bash in /nix/store/.../bin/bashdoes not workworks
Plain Debian/Ubuntuworksworks

The exception is #!/bin/sh. POSIX guarantees that a POSIX shell is always at /bin/sh. If your script is genuinely POSIX-compatible, write /bin/sh and save yourself the trouble.

Arguments to the interpreter

A shebang can pass one argument (a Linux limit):

bash
#!/usr/bin/env -S bash -euo pipefail

▸-S splits the line into tokens (without -S the whole "bash -euo pipefail" goes through as ONE argument)

With env -S (needs coreutils 8.30+) you can wedge strict mode straight into the shebang. The alternative is set -euo pipefail as the first line of the script.

Not only bash

A shebang works with any interpreter:

python
#!/usr/bin/env python3
print("hello from python")
ruby
#!/usr/bin/env ruby
puts "hi"
nodejs
#!/usr/bin/env node
console.log('hi')

The /usr/bin/env <interpreter> idiom is universal. It works for all of them.

Common mistakes

  • CRLF (Windows line endings): the kernel sees #!/usr/bin/env bash\r and tries to find an interpreter called bash\r. The error is bad interpreter: No such file or directory. Fix: dos2unix file.sh.
  • No +x bit: run chmod +x file.sh.
  • No full path: #!bash does not work, you need an absolute path.
  • Shebang not on the FIRST line: the kernel looks only at the first two bytes, so a blank line before the shebang breaks the magic.

§ команды

bash
head -1 script.sh

Look at the shebang of an existing script

bash
file script.sh

Shows the file type and which interpreter the shebang names, handy for binaries too

bash
chmod +x script.sh && ./script.sh

Make it executable and run it, the standard pipeline

bash
dos2unix script.sh

Strip Windows CRLF when the file came from a Windows editor

§ см. также

  • process-and-pidProcess and PIDA process is a running program with its own PID, memory, open descriptors, and UID. Every process forms a tree rooted at init (PID 1).
  • file-permissionsFile permissions: rwx and chmodEvery file has three permission sets: for the owner, the group, and others. Each set is three bits: read (r), write (w), execute (x). You change them with `chmod`.
  • fhsFilesystem Hierarchy Standard (FHS)FHS is the standard for what lives in which Linux root directory: /etc holds config, /var holds changing data, /usr holds the distro's static files, /home holds users, /tmp holds temporary files.
  • bash-strict-modeBash strict mode: set -euo pipefailThree flags at the top of a bash script that turn it from forgiving into fail-on-the-first-error. Without them, bugs pile up silently.

§ упоминается в уроках

  • ›beginner-12-shell-scripting
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies