basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetLinuxCommand line

    Bash aliases and functions: commands on one screen

    Define, inspect, bypass, remove, check what loads over SSH, and wire up Tab completion. How it works — Bash aliases and Bash functions.

    Define

    Command What it does
    alias ll='ls -alF' alias for the rest of the session
    alias ..='cd ..' dots in the name are allowed
    alias sudo='sudo ' expand aliases after sudo too
    duh() { du -h -d1 "${1:-.}" 2>/dev/null | sort -h; } one-line function
    echo "alias ports='ss -tulpn'" >> ~/.bash_aliases append an alias permanently
    source ~/.bash_aliases reload the file in this terminal

    No spaces around =, and wrap the value in single quotes. A one-line function needs a space after { and a ; before }.

    Inspect

    Command What it does
    alias all aliases in this shell
    alias ll one alias: alias ll='ls -alF'
    type ll what the name points to: alias, function, or program
    type -a ls every match under this name; the first one runs
    type duh full function definition

    On Ubuntu, type -a ls prints the alias, then /usr/bin/ls and /bin/ls. Both paths point to the same file: /bin is a symlink to /usr/bin.

    Bypass and remove

    Command What it does
    \ls run without the alias
    command ls bypass both alias and function
    unalias ll remove an alias for the rest of the session
    unalias -a remove all aliases in this shell
    unset -f duh remove a function
    exec bash fresh shell from what is currently in your files

    When names collide, lookup order is: alias, function, Bash builtin, then external program. source doesn’t remove an alias deleted from the file — it only adds or overwrites. exec bash reads everything from scratch, but drops unexported variables from the current session.

    Check what loads

    Command What it does
    grep bash_aliases ~/.bashrc whether ~/.bash_aliases is sourced
    bash -lic 'type ports' whether the alias loads on SSH login
    ls ~/.bash_profile whether a file exists that makes SSH login skip .profile
    sort ~/.bash_history | uniq -c | sort -rn | head -20 your most frequent commands — candidates for aliases

    Bash writes the current session’s commands to ~/.bash_history on exit; flush them immediately with history -a.

    In scripts

    Command What it does
    shopt expand_aliases whether aliases expand: on in a terminal, off in a script
    shopt -s expand_aliases enable aliases in a script
    export -f duh pass a function to child Bash processes in this session

    You can’t pass a function through sudo even with export -f: sudo runs programs only, and sudo bash -c 'duh' returns duh: command not found.

    Tab completion

    Command What it does
    complete -p systemctl which function completes the command
    _completion_loader systemctl load the command’s completion right away
    complete -F _systemctl sc attach the same completion to alias sc
    complete -F _complete_alias dc for an alias with arguments, via complete-alias

    Put alias completion lines in ~/.bash_completion, not ~/.bash_aliases (Tab after an alias).

    Shortcuts

    Keys What they do
    Ctrl+Alt+E or Esc, then Ctrl+E expand aliases on the command line without running
    Ctrl+L clear the screen
    Ctrl+R search history; press again for the next match