basforge

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

    RU

    Bash aliases: your own short commands

    An alias is a short word that Bash substitutes for a command: type ports, and it runs ss -tulpn. Keep your aliases in ~/.bash_aliases — Ubuntu loads that file on its own. Aliases only work where you type commands by hand: scripts and ssh web1 'command' don’t see them.

    Why set up short commands

    Some server commands get typed several times a day, each with its own flags. ss -tulpn lists every listening port and the process behind it: drop -n and port 53 turns into :domain, drop -p and the process column disappears. docker compose logs -f --tail 100 follows the tail of a log: without --tail, Compose dumps the entire stored log first. Keeping flags in your head gets old fast, and a missed flag silently shows the wrong thing.

    An alias saves a command and its flags once under a short name. Type ports, and Bash replaces it with ss -tulpn before running it. Ubuntu uses aliases out of the box: ls prints colored output in a terminal because it’s aliased to ls --color=auto.

    How Bash expands an alias

    Bash reads the line and checks the first word of each command. If that word is an alias, Bash swaps in the alias text and leaves the rest of the line untouched:

    typed:  ll /etc
    runs:   ls -alF /etc
    • Only the first word of a command expands: at the start of a line, or right after ;, &&, |. cd /etc && ll works, while echo ll prints ll.
    • Arguments attach to the end of the alias text. For ls -alF that’s what you want; for an alias containing a pipe it isn’t — more on that below.
    • An alias takes precedence over a function, a shell builtin, and an external program with the same name. To bypass it, run \ls or command ls: that runs the program directly without the alias flags.
    • Aliases only apply in an interactive shell where you type commands. Scripts, cron, and ssh web1 'command' don’t see them.

    Check what a name resolves to:

    type ll
    # ll is aliased to `ls -alF'
    alias
    # list all aliases in this shell

    You can expand a command line in place without running it: type the line and press Ctrl+Alt+E (if your terminal intercepts that shortcut, press Esc then Ctrl+E). ll /etc turns into ls -alF /etc right on the prompt. Variables and !! expand at the same time.

    What Ubuntu already includes

    For every new user, Ubuntu copies /etc/skel/.bashrc to ~/.bashrc. It already defines several aliases:

    Alias What it runs
    ls, grep, fgrep, egrep the same command with --color=auto: color in a terminal, plain text in files and pipes
    ll ls -alF — all files, long format, / on directories, * on executables
    la ls -A — includes hidden files, skips . and ..
    l ls -CF — columns, with / and *
    alert desktop notification after a long command: sleep 10; alert. Doesn’t work on a server — no notify-send

    It also includes the lines that load your own file:

    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi

    Keep your aliases in ~/.bash_aliases rather than editing .bashrc. That leaves .bashrc stock, and your aliases copy to another server in one command and work right away.

    Root has its own /root/.bashrc — from the base-files package — with the same ll, la, l, and the same ~/.bash_aliases check, except ~ for root is /root.

    On other distributions, check with grep bash_aliases ~/.bashrc. If nothing comes back, append those three lines to the end of ~/.bashrc.

    Your own aliases: from history to servers

    1. Find what you type most often

      history -a
      sort ~/.bash_history | uniq -c | sort -rn | head -20

      history -a flushes commands from the current session to disk, since Bash otherwise writes them only on exit. Ubuntu stores the last 2,000 lines (HISTFILESIZE=2000 in .bashrc). Good candidates are long commands you repeat and commands whose flags you keep looking up.

    2. Check whether the name is free

      type dc
      # bash: type: dc: not found

      not found means the name is free. If a program already has that name, your alias will shadow it. Only reuse a standard command’s name when the alias adds display flags, like ls --color=auto. A flag that changes behavior breaks pipelines you rely on: with alias grep='grep -rn', running ps aux | grep sshd searches files in the current directory and ignores the output of ps.

    3. Add aliases to ~/.bash_aliases

      nano ~/.bash_aliases
      # listening ports and which process owns each
      alias ports='ss -tulpn'
      alias dc='docker compose'
      alias ..='cd ..'

      Put no spaces around =. Wrap the value in single quotes: inside double quotes, $… and $(…) expand once when Bash reads the file. alias today="echo $(date +%F)" prints the day you logged in even if the terminal has been open since last week.

    4. Reload the file in this terminal

      source ~/.bash_aliases

      New and edited aliases take effect right away. Other open terminals pick them up after the same command or a new login. An alias deleted from the file stays active in the current terminal until you run unalias name.

    5. Copy it to a server

      scp ~/.bash_aliases web1:

      On Ubuntu nothing else is needed: the aliases are ready on your next SSH login. For root, make a separate copy on the server: sudo cp ~/.bash_aliases /root/.

    Alias, function, or script

    An alias pastes text and appends any arguments to the end. That fits a single command with fixed flags, but breaks as soon as a pipe is involved:

    alias duh='du -h -d1 2>/dev/null | sort -h'
    duh /etc
    # sort: read failed: /etc: Is a directory

    /etc went to sort instead of du. Whenever an argument belongs in the middle, write a function — in the same ~/.bash_aliases file:

    # disk usage by directory, largest at the bottom
    duh() { du -h -d1 "${1:-.}" 2>/dev/null | sort -h; }

    $1 is the first argument, and ${1:-.} falls back to the current directory when none is passed.

    The second reason to use a function is cd. A script runs in a child process, so its cd only changes the directory inside that child process. A function runs inside your shell, leaving you in the new directory:

    # create a directory and enter it
    mkcd() { mkdir -p "$1" && cd "$1"; }

    Functions and aliases share one file. ~/.bash_aliases is a regular Bash file: .bashrc executes all of it, so a function defined there is available in your terminal just like an alias and called by name. An alias can call a function — alias dlog='duh /var/log'. A function can use an alias if that alias appears earlier in the file, so put aliases first and functions below them. For how to write functions, what $1 and "$@" mean, and where functions don’t work, see Bash functions.

    If… Use
    one command with fixed flags and arguments at the end an alias
    an argument in the middle, a pipe, cd, or several commands a function in ~/.bash_aliases
    the command is needed from cron, other scripts, or by other users a script in ~/.local/bin: Ubuntu adds it to PATH at login once the directory exists
    a short server name instead of ssh [email protected] Host web1 in ~/.ssh/config: scp, rsync, and git understand it too (~/.ssh/config)
    a shortcut for git [alias] in ~/.gitconfig: git lg works in any shell that has git (~/.gitconfig block by block)

    Where aliases get in the way

    An alias lives in a file on one machine. On a machine without that file it doesn’t exist: ll is present on every Ubuntu host, while ports and dc exist only where you copied ~/.bash_aliases. Safety-net aliases bite hardest: if you rely on alias rm='rm -i', on someone else’s server you’ll delete files without a confirmation prompt (rm -I instead of rm -i).

    A ready-to-use everyday file with notes on each line is in Everyday aliases; less obvious aliases are in Lesser-known aliases.

    Pressing Tab after an alias completes filenames instead of subcommands: systemctl sta + Tab completes start or status, while sc sta + Tab does nothing. Bash completion attaches to the command name, not the alias. To wire it up, see Tab after an alias.

    Commands with aliases won’t work for anyone else. In docs, scripts, and messages to colleagues, write the full command: docker compose up -d, not dc up -d. All guides on this site use full commands for the same reason.

    If it doesn’t work

    ssh web1 ll replies bash: line 1: ll: command not found

    Cause
    A command after the host runs in a non-interactive Bash. It doesn’t expand aliases, and Ubuntu’s .bashrc exits in its first lines in that mode.
    Fix
    Log in with ssh web1 and run it inside the session. For a single command over SSH, write it out in full: ssh web1 'ls -alF'.

    sudo ll replies sudo: ll: command not found, or with sudo-rs on Ubuntu 26.04 — sudo: 'll': command not found

    Cause
    Bash only expands the first word, which here is sudo. It passes ll to sudo as a program name, and no such program exists.
    Fix
    Add alias sudo='sudo ' to ~/.bash_aliases: the trailing space tells Bash to expand the next word too. Only the next word, though: sudo -u root ll still fails because -u comes right after sudo.

    Under sudo -i your aliases are missing, and only ll, la, l work

    Cause
    Root reads /root/.bashrc, which loads /root/.bash_aliases. Your file sits in another home directory.
    Fix
    Put a copy in root’s home: sudo cp ~/.bash_aliases /root/.

    Aliases work in a desktop terminal window, but not over SSH

    Cause
    An SSH login starts a login shell. It reads the first existing file among ~/.bash_profile, ~/.bash_login, and ~/.profile, and on Ubuntu .profile is what loads .bashrc. If ~/.bash_profile exists, .profile is never read.
    Fix
    Put . ~/.profile as the first line of ~/.bash_profile: that pulls in both .bashrc and PATH with ~/.local/bin.

    A script fails with ll: command not found, even though ll works in the terminal

    Cause
    Scripts run in a non-interactive Bash, which doesn’t expand aliases.
    Fix
    Write the full command in scripts. shopt -s expand_aliases turns alias expansion on, but then the script only works where your ~/.bash_aliases exists.

    You edited ~/.bash_aliases, but an open terminal still uses the old version

    Cause
    Bash reads the file once, at startup.
    Fix
    Run source ~/.bash_aliases. An alias you removed from the file stays active until you run unalias name or start a new session.

    alias ll = 'ls -alF' prints bash: alias: ll: not found and bash: alias: =: not found

    Cause
    Spaces around =: Bash sees three separate words and tries to print an existing alias for each.
    Fix
    Write it without spaces: alias ll='ls -alF'.

    An alias with a pipe and a path prints sort: read failed: /etc: Is a directory

    Cause
    The argument gets appended to the end of the whole alias text and lands on the last command in the pipe.
    Fix
    Use a function instead — see the “Alias, function, or script” section.

    How to check

    Confirm the alias is loaded:

    type ports
    # ports is aliased to `ss -tulpn'

    Check whether it will be there on an SSH login without leaving your session. bash -lic starts a login shell that reads the same startup files as SSH:

    bash -lic 'type ports'
    # ports is aliased to `ss -tulpn'