Understanding the Linux Shell
What Is a Shell in Simple Terms
The shell is the layer between you and the Linux kernel. When you type ls, you are not talking directly to the kernel — you are talking to the shell, which interprets your command, calls the right kernel functions, and displays the result.
Every terminal session on a Linux server runs a shell. When you SSH into a production server at Swiggy, the first thing you see is a bash prompt — that is the shell, ready for commands.
How It Works
+------------------------------------------+| You type: ls -la /var/log/ |+------------------------------------------+ | v+------------------------------------------+| Shell (bash) parses the command || Expands wildcards, variables, aliases || Finds ls binary: /usr/bin/ls |+------------------------------------------+ | v+------------------------------------------+| Kernel executes /usr/bin/ls || Reads directory entries from filesystem || Returns output to shell |+------------------------------------------+ | v+------------------------------------------+| Shell displays output to terminal |+------------------------------------------+Shell types and when to use each:
## Check which shell you are usingecho $SHELL## /bin/bash ## Check current shell (may differ from login shell)echo $0## bash ## bash -- default on most Linux servers## Best for: scripting, automation, DevOps workbash --version ## sh -- POSIX-compatible minimal shell## Best for: portable scripts that run on any Unixsh --version ## zsh -- extended bash with better tab completion## Best for: developer workstationszsh --version ## fish -- user-friendly but not POSIX-compatible## Best for: interactive use, not scriptingLogin shell vs interactive shell vs non-interactive:
Login shell (SSH session, su -): Sources: /etc/profile -> ~/.bash_profile -> ~/.bashrc Use: full environment setup Interactive non-login shell (new terminal in existing session): Sources: ~/.bashrc only Use: daily terminal work Non-interactive shell (scripts, cron jobs): Sources: nothing by default Use: automation -- PATH is minimal, be explicitPractical Commands
## List available shellscat /etc/shells ## Change login shellchsh -s /bin/zsh ## Run a command in a specific shellbash -c 'echo $BASH_VERSION'sh -c 'echo hello from sh' ## Check if running interactively in a scriptif [ -t 0 ]; then echo "Running interactively"else echo "Running non-interactively (script or pipe)"fiTroubleshooting
| Symptom | Command | What to Check |
|---|---|---|
| Command not found | which commandname |
Not in PATH for this shell |
| Script works manually not in cron | echo $SHELL in cron |
Cron uses /bin/sh not bash |
| Alias not in script | type aliasname |
Aliases not exported to sub-shells |
PLACEMENT PRO TIP**Tip:** Always use `#!/usr/bin/env bash` as the shebang line in scripts instead of `#!/bin/bash`. The `env` version finds bash wherever it is installed, making scripts portable across systems where bash may be at a different path.
REMEMBER THIS**Remember:** Scripts run by cron use a non-interactive, non-login shell. None of your `.bashrc` aliases, functions, or PATH additions are available. Always use absolute paths in cron scripts.