Linux Programming and Cloud Computing

Shell Environment, Expansion, Redirection and Pipelines

PGCP-BDA

shell and Bash

A shell parses command language, performs expansions and redirections, launches programs and reports status

shell variable and environment

A shell variable belongs to the current shell

startup files

Shell startup files initialize login or interactive sessions

quoting

Shell quoting controls interpretation

parameter expansion

Parameter expansion substitutes or transforms a shell parameter and can provide defaults, require values, remove patterns or compute string lengths.

command substitution

Command substitution runs a command in a subshell context, removes trailing newlines from its standard output and inserts the remaining text into the.

globbing

Shell pathname expansion that replaces *, ? and bracket patterns with matching directory entries before command execution.

standard streams

Every process conventionally begins with file descriptors 0, 1 and 2 for standard input, standard output and standard error.

redirection

Redirection changes which open file a command uses for standard input, output or error

pipeline

A group of concurrent commands in which each command’s standard output feeds the next command’s standard input.

exit status

A process returns an integer status to its parent; zero conventionally means success and a nonzero value identifies a failure class.

Shell Parsing and Expansion

The shell recognizes quoting and operators, performs expansions and then starts commands. Variables use $name or ${name}. export places a variable in the environment inherited by child processes. Command substitution $(command) captures standard output. Arithmetic expansion $((expression)) evaluates integer arithmetic. Filename generation expands patterns such as *.csv.

Single quotes preserve all enclosed characters. Double quotes still allow parameter, command and arithmetic expansion but prevent word splitting and filename generation. A backslash protects one following character. Values that may contain spaces or wildcard characters must be quoted.

Streams, Redirection and Pipes

A process normally has standard input on descriptor 0, standard output on 1 and standard error on 2. > replaces an output file while >> appends. < supplies input. 2> redirects diagnostics. 2>&1 makes standard error use the current standard-output destination, so redirection order matters.

A pipeline connects one command’s standard output to the next command’s standard input. Standard error is not included unless redirected. Exit status normally belongs to the last command; pipefail can expose failure in an earlier stage. Startup environments differ between interactive terminals, login shells and service processes. Scripts should use explicit inputs and should not depend on terminal aliases or undeclared environment values.

Expansion Order and Command Grouping

Parameter expansion supports defaults and validation. ${name:-value} supplies a default when the variable is unset or empty. ${name:?message} stops with a diagnostic if a required value is absent. ${#name} returns string length. These forms reduce scattered tests but should remain readable.

Brace expansion such as {1..5} and file.{csv,json} is textual generation performed before filename matching. A glob pattern matches pathnames while a regular expression describes text for tools that support it; they are different languages. An unmatched glob may remain literal depending on shell options.

Parentheses run a command group in a subshell, so directory and variable changes do not affect the parent shell. Braces group commands in the current shell and require correct separators. cmd1 && cmd2 runs the second command only after success. cmd1 || cmd2 runs it after failure. A semicolon runs the next command regardless of status. Background & returns control to the shell while the command continues.

Here-documents supply multiple input lines. A quoted delimiter prevents expansion inside the body. tee copies input to a file and standard output, which is useful when a pipeline must both store and continue data. Redirection is prepared by the shell before a command starts, so a privileged command does not make an unprivileged shell redirection privileged.

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.