programming

Bash mktemp: Create Temporary Files and Directories Safely

Learn how Bash mktemp creates temporary files and directories safely, use templates and TMPDIR, register trap EXIT cleanup, avoid unsafe mktemp -u, and atomically replace files on the same filesystem.

Deepak Prasad9 min read
programming

Logging in Bash Scripts

Learn Bash script logging: stdout vs stderr, a minimal log function with levels and timestamps, tee, exec redirects, logger for syslog/journald, systemd capture, rotation, and keeping secrets out of logs.

Deepak Prasad14 min read
programming

Bash set vs shopt: How Shell Options Work

Learn why Bash has both set and shopt, how to enable and check shell options, and when to use nullglob, failglob, lastpipe, and inherit_errexit in scripts.

Deepak Prasad12 min read
programming

Bash Error Handling: Practical Patterns for Reliable Scripts

Learn Bash error handling with exit codes, set -e exceptions, trap ERR, expected grep and diff statuses, bounded retries with backoff, batch partial failure, stderr diagnostics, and trap EXIT cleanup.

Deepak Prasad15 min read
programming

Bash Strict Mode: `set -euo pipefail` Explained

Learn what set -euo pipefail does in Bash, when set -e does not exit, how nounset and pipefail behave, and the common strict-mode pitfalls in functions, pipelines, and command substitution.

Deepak Prasad13 min read
programming

How to Read a File Line by Line in Python

Learn how to read a text file line by line in Python using file iteration and readline(), handle newlines and encodings, add line numbers, and process large files without loading the entire file into memory.

Deepak Prasad14 min read
programming

Bash Subshells: () vs {}, Variable Scope and Examples

Learn how Bash subshells work, when ( ) isolates variables and cd, how { } differs, and why pipelines and command substitution can lose shell state.

Deepak Prasad13 min read
programming

Bash Process Substitution: <() and >()

Bash process substitution with <() and >(): expose command output as a file path, compare pipes and $(), keep while read in the current shell, and handle async exit status.

Deepak Prasad10 min read
programming

Bash File Descriptors and exec

Open Bash file descriptors beyond 0/1/2 with exec, write with >&3, read with read -u, save and restore stdout, use dynamic {fd} allocation, and close FDs to avoid leaks.

Deepak Prasad14 min read
programming

Bash Heredoc: <<EOF, Variables, Indentation and Here Strings

Learn Bash heredoc and here-string syntax: quoted delimiters, <<- tab stripping, variable assignment, sudo tee, SSH expansion, and tested <

Deepak Prasad12 min read
programming

Bash Redirection: stdin, stdout and stderr

Bash redirection explained with file descriptors 0, 1, and 2, left-to-right 2>&1 semantics, split logs, append, exec, and why sudo does not fix a failed shell redirect.

Deepak Prasad11 min read
programming

Bash Pipes: pipefail, PIPESTATUS and Exit Codes

Learn Bash pipes: connect stdout between commands, use pipefail and PIPESTATUS for pipeline exit codes, pipe stderr with |&, and avoid subshell and SIGPIPE surprises.

Deepak Prasad11 min read
programming

Bash Parameter Expansion: ${var:-}, Substrings, Trim & Replace

Bash parameter expansion explained with ${var:-default}, substring slicing, trim, replace, and a truth table for default, assignment, alternate, and error operators covering unset vs empty variables.

Deepak Prasad11 min read
programming

Bash Expansion Order: How Shell Expansion Works

Learn Bash expansion order from brace and variable expansion to word splitting, globbing, and quote removal, with tested examples.

Deepak Prasad13 min read
programming

Bash Command Substitution: Syntax, Examples and $() vs Backticks

Learn Bash command substitution with $(): capture command output in variables, compare backticks, handle quoting and newlines, and understand subshell behavior.

Deepak Prasad9 min read
programming

Split a String into an Array in Bash

Split one Bash string into array elements with IFS and read -ra, handle comma colon and pipe delimiters, preserve spaces, avoid unsafe arr=($string) globbing, and know when IFS is not a CSV parser.

Deepak Prasad10 min read
programming

Bash IFS and Word Splitting Explained

Learn how Bash IFS controls word splitting on unquoted expansions, how whitespace delimiters differ from commas and colons, and safe patterns with read, local IFS, and "$@".

Deepak Prasad12 min read
programming

Bash mapfile / readarray: Read Lines into an Array

Load files and command output into Bash indexed arrays with mapfile and readarray: -t for clean lines, -n and -s for limits, -O for a starting index, -d for custom delimiters, and NUL-safe find -print0 paths.

Deepak Prasad9 min read
programming

Bash Associative Arrays: Key-Value Maps

Create Bash associative arrays with declare -A, store string keys and values, loop keys safely, count occurrences, use maps as sets, and avoid common quoting and ordering mistakes.

Deepak Prasad12 min read
programming

Bash Arrays: Create, Append, Loop, Slice and Delete

Learn Bash indexed arrays: create, access, append, loop, slice, and delete elements safely with "${array[@]}", sparse indices, negative indexes, and command argument arrays.

Deepak Prasad11 min read
programming

Bash getopts: Parse Short Options and Handle Long Options

Learn Bash getopts for short flags, OPTARG and OPTIND, silent error mode, shift after parsing, reusable functions, manual long options, and when to use GNU getopt instead.

Deepak Prasad14 min read
programming

Bash source Command: Source vs Execute a Script

Learn when to use Bash source or dot versus bash file or ./file, what changes persist in the current shell, how sourced arguments and return status work, PATH lookup with sourcepath, and Bash 5.3 source -p path.

Deepak Prasad12 min read
programming

Bash Functions: Arguments, Variables and Return Values

Define Bash functions with arguments, local variables, return status, stdout capture, and nameref output parameters, including dynamic scope and subshell behavior.

Deepak Prasad13 min read
programming

Create Interactive Menus with Bash select

Build numbered terminal menus in Bash with select, customize the PS3 prompt, read choices from REPLY, validate invalid input, use arrays and function arguments, and add a quit option with break.

Deepak Prasad11 min read
programming

Bash break and continue

Use Bash break and continue to exit loops early or skip an iteration, including nested levels, while read parsing, and select menus.

Deepak Prasad10 min read
programming

Read a File Line by Line in Bash

Read a text file line by line in Bash with while IFS= read -r, preserve whitespace and backslashes, handle files without a final newline, and avoid pipeline subshell traps.

Deepak Prasad9 min read
programming

Bash while Loop

Use Bash while loops with command exit status, arithmetic counters, infinite loops, retries, brief read loops, the pipeline subshell trap, until, and do-while emulation.

Deepak Prasad11 min read
programming

Bash Wildcards and Globbing

Learn Bash globbing and wildcards with tested examples for *, ?, bracket patterns, nullglob, dotglob, nocaseglob, globstar, extglob, quoting, arrays, and safe loops over matched files.

Deepak Prasad13 min read
programming

Bash for Loop: Syntax and Examples

Learn Bash for loop syntax with practical examples for lists, arrays, files, script arguments, numeric ranges, and C-style counters.

Deepak Prasad12 min read
programming

Bash Regex: =~ and BASH_REMATCH

Match text in Bash with [[ =~ ]], capture groups in BASH_REMATCH, store regex in variables, avoid RHS quoting traps, and understand POSIX ERE limits versus PCRE.

Deepak Prasad9 min read