Table of Contents


Compiled vs. Interpreted

  • sh is a compiled binary, often written in C.
  • .sh is a shell script — a plain text file interpreted by a shell like sh, bash, or zsh.
  • Compiled executables can run directly. Scripts require an interpreter.

Shebang

  • A shebang (#!) tells the shell which program to use to run the script.
  • Syntax:
#! interpreter [optional-arg]

Examples:

#!/bin/bash       # Bash
#!/usr/bin/zsh    # Zsh
#!/usr/bin/python3  # Python 3
  • Without a shebang, scripts need to be run like: bash script.sh

Config Files

  • Used to configure the shell at startup:

    • Bash: ~/.bashrc
    • Zsh: ~/.zshrc

These files can set:

  • Environment variables
  • Aliases
  • PATH modifications

Environment Variables

  • Available to all child processes of the shell.
  • View current variables:
env
  • Set a variable:
export NAME="Adarsh"
echo $NAME
  • Scripts can access exported variables:

introduce.sh:

#!/bin/sh
echo "Hi I'm $NAME"

Make it executable:

chmod +x introduce.sh
./introduce.sh  # Hi I'm Adarsh

PATH Variable

  • A colon-separated list of directories the shell checks to find commands.
echo $PATH

Example:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  • Without PATH, you’d have to run /bin/ls instead of just ls.

Modifying PATH

  • Append a new directory to your current PATH:
export PATH="$PATH:/your/custom/path"
  • Add this to ~/.bashrc or ~/.zshrc to make it persistent across sessions.

This site uses Just the Docs, a documentation theme for Jekyll.