A Bash alias is a shell built-in mechanism that performs lexical substitution on the first word of a simple command during the shell’s command parsing phase. When the shell reads input, it checks the first unquoted word against an internal hash table of defined aliases. If a match is found, the shell replaces the alias name with its corresponding string value before parsing the rest of the command line for execution.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax
Thealias built-in defines or displays aliases.
=).
Quoting and Evaluation
The choice of quotes surrounding thevalue dictates when variable expansion occurs:
- Single Quotes (
'...'): Variables are evaluated at execution time. The literal string is stored in the alias hash table. - Double Quotes (
"..."): Variables are evaluated at definition time. The expanded result is stored in the alias hash table.
Self-Referential Aliases and Recursion Prevention
Bash explicitly prevents infinite recursion when an alias references its own name. If the replacement string contains the alias’s own name as the first word, the shell flags that specific alias name as unexpandable for the remainder of that substitution. This allows an alias to safely act as a wrapper for a command of the exact same name.Chaining and Trailing Spaces
By default, Bash only attempts alias expansion on the first word of a command. However, if the aliasvalue ends with a trailing space or tab, Bash will also attempt alias expansion on the immediately following word in the command line.
Bypassing Alias Expansion
To suppress alias expansion and force the shell to execute the underlying command or binary, the alias name can be quoted, escaped, or prefixed with thecommand built-in.
Scope and Execution Context
- Memory-Bound: Aliases are strictly ephemeral and exist only within the memory space of the current shell session.
- Subshells vs. Child Processes: Subshells (created via
( ... ), command substitution$( ... ), or backgrounding&) fork the parent process and inherit its entire memory state. Therefore, all aliases defined in the parent shell are present and functional in the subshell. Conversely, aliases are not exported to external child processes (such as a newly invokedbashinstance) because they cannot be inherited via the environment. - Non-Interactive Shells: By default, alias expansion is disabled in non-interactive shells (such as shell scripts). To utilize aliases within a script, the behavior must be explicitly enabled using the shell options built-in:
Parse-Time Limitations
Because alias expansion occurs strictly at parse time rather than execution time, an alias becomes available only after the shell has completely parsed the line or compound command where it was defined. Consequently, an alias defined within a compound command (such as anif block, for loop, or function) or on the same line as its invocation cannot be used until the next line or block of input is read.
Argument Handling Limitations
Unlike shell functions, aliases do not support positional parameters (e.g.,$1, $2). They are strictly limited to static string substitution. When an alias is invoked with arguments, the shell performs a lexical substitution of the first word in the input stream, and the remaining words on the command line are parsed normally as separate tokens following the expanded alias string.
Removal
To remove an alias from the current shell’s hash table, use theunalias built-in.
Master Bash with Deep Grasping Methodology!Learn More





