Skip to main content

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.

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.

Syntax

The alias built-in defines or displays aliases.

# Definition syntax
alias name='value'


# Display a specific alias
alias name


# Display all defined aliases
alias
Strict Formatting: There must be absolutely no whitespace on either side of the assignment operator (=).

Quoting and Evaluation

The choice of quotes surrounding the value 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.

# Evaluated when the alias is invoked
alias cmd_dynamic='echo $VAR'


# Evaluated immediately when the alias is defined
alias cmd_static="echo $VAR"

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.

# The 'ls' inside the replacement string is not expanded a second time
alias ls='ls --color=auto'

Chaining and Trailing Spaces

By default, Bash only attempts alias expansion on the first word of a command. However, if the alias value ends with a trailing space or tab, Bash will also attempt alias expansion on the immediately following word in the command line.
alias prefix='command_1 '
alias target='command_2'


# Because 'prefix' ends with a space, 'target' will also be expanded
prefix target 

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 the command built-in.

# Escape the first character
\name


# Quote the alias name
'name'
"name"


# Use the command built-in to bypass function and alias lookup
command name

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 invoked bash instance) 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:
shopt -s expand_aliases

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 an if 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.

# Fails: The alias is not available during the parsing of this single line
alias foo='echo bar'; foo 


# Fails: The alias is not available until the entire 'if' block is parsed
if true; then
    alias baz='echo qux'
    baz
fi

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 the unalias built-in.

# Remove a specific alias
unalias name


# Remove all aliases in the current session
unalias -a
Master Bash with Deep Grasping Methodology!Learn More