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 hostnamectl Command: Show and Change the Host Name

Linux hostnamectl Command: Show and Change the Host Name

Learn how to show and Change the Host Name with the Linux hostnamectl command, including practical examples, key options, and important precautions.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux tail Command: View the End of a File and Follow Logs

Linux tail Command: View the End of a File and Follow Logs

Learn how to use the Linux tail command to view the last lines of a file and follow log updates in real time, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

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 watch Command: Run a Command Repeatedly

Linux watch Command: Run a Command Repeatedly

Learn how to use the Linux watch command to rerun a command at regular intervals and highlight changing output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dmesg Command: Read Kernel and Boot Messages

Linux dmesg Command: Read Kernel and Boot Messages

Learn how to read Kernel and Boot Messages with the Linux dmesg command, including practical examples, key options, and important precautions.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux renice Command: Change the Priority of a Running Process

Linux renice Command: Change the Priority of a Running Process

Learn how to change the Priority of a Running Process with the Linux renice command, including practical examples, key options, and important precautions.