Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Bash reads a command, resolves its name and arguments, performs expansions and redirections, and then runs it. A semicolon ; runs the next command regardless of the previous result, && runs it only after success, and || runs it only after a nonzero status. Aliases change command text as the shell reads it; environment variables pass settings to child processes. These distinctions explain why similar-looking command lines can behave differently.

How is a command name resolved?

The examples assume Bash. The shell reads the command line, handles arguments, variables, and redirections, and resolves the command name. An alias, function, shell built-in, or executable found through PATH can produce different behavior. Inspect the current shell's resolution with:

type -a printf
type -a ls
command -v ls

type -a can show aliases, functions, built-ins, and executable candidates. command -v checks whether a name can be resolved. PATH lists directories searched for external executables; it does not explain every alias or function as one file path. Do not overwrite your system's PATH for this exercise.

Use exit status to control the next command

A shell command leaves an exit status. By convention, 0 indicates success; a nonzero value indicates failure or another defined outcome. For example, grep can return 1 when nothing matches. $? contains the most recent command's status, so check it immediately.

false
echo $?

Bash's false returns a failure status, so the output is 1. These operators use that status:

Operator When the right command runs Typical use
; Regardless of the left result Sequence commands
&& When the left status is 0 Continue after success
|| When the left status is nonzero Run an alternative after failure
false && printf 'not run\n'
false || printf 'fallback\n'
true; printf 'always\n'

Run these lines in order: not run is not printed, but fallback and always are. In Bash, && and || have equal precedence and associate from left to right. Thus task && success_action || failure_action may run the failure action if the success action itself fails. Use if ...; then ...; else ...; fi when you need an unambiguous two-way branch.

Aliases expand when Bash reads a command

alias creates a short command name commonly used in interactive Bash. Enter the following definition, then run ll or type ll at a new prompt. Bash reads and parses commands before executing them, so defining and using an alias in the same compound command may not expand as expected.

alias ll='ls -l'
type ll
ll

An alias normally affects only the current shell and is not expanded in noninteractive Bash unless enabled separately. Do not make an automation script depend on an interactive alias by accident. Remove this practice alias with unalias ll. For reusable operations needing arguments, consider a shell function rather than an alias.

Distinguish shell variables from the environment

A shell variable is available in the current shell. Once exported, it is also placed in the environment of subsequently launched child processes. Use a practice-only name rather than a common system variable:

LESSON_MODE=practice
printf '%s\n' "$LESSON_MODE"
export LESSON_MODE
printenv LESSON_MODE

Both outputs are practice. The first comes from expansion in the current shell; the second is read from the environment passed to the external printenv process. Exporting does not retroactively change an already-running other shell or a parent process.

To supply a value for one command only, use NAME=value command. When finished, run unset LESSON_MODE in the current shell. Do not repurpose your home-directory or system PATH variable as a practice name.

Troubleshoot execution order

  • If you do not know how a name resolves, begin with type -a name.
  • If conditional execution surprises you, examine each stage's exit status separately.
  • If an alias fails, check that Bash is interactive and that you used it on a new command line after defining it.
  • If a child command cannot see a variable, check whether it was exported.

Sequencing operators, aliases, and variables all affect execution, but they are not interchangeable. Before connecting commands that change files, test the flow with low-impact built-ins such as true, false, and printf.

Official documentation

The GNU Bash lists documentation covers ;, &&, and ||. The Bash aliases documentation covers expansion timing. For variables and environment behavior, see the Bash Reference Manual.

More in This Category
Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux file Command: Identify File Types and Formats

Linux file Command: Identify File Types and Formats

Learn how to use the Linux file command to identify file formats from their contents instead of filename extensions, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux Tutorial / Read Files with cat, less, more, head, and tail

Linux Tutorial / Read Files with cat, less, more, head, and tail

Learn when to use cat, less, more, head, and tail to read Linux files, with a small reproducible example and guidance for long files and logs.

Linux date Command: Display and Format the Current Time

Linux date Command: Display and Format the Current Time

Learn how to display and Format the Current Time with the Linux date command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux rm Command: Remove Files and Directories Safely

Linux rm Command: Remove Files and Directories Safely

Learn how to remove files and directory trees with Linux rm, use interactive safeguards, limit recursive deletion, and avoid destructive path mistakes.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.