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.









