Skip to content

Bash Practices

The scripts in this section follow a few conventions. They cover how the files are named, where they are installed, and when a job calls for an alias, a function, or a script. This page explains each one so you can apply them to scripts of your own.

File extensions

Google's Shell Style Guide allows an executable to have a .sh extension or none. It prefers no extension when the executable goes directly on the user's PATH. Libraries must have a .sh extension and should not be executable.1

Omit the extension when the script is a command:

  • It is installed on the PATH (~/.local/bin, ~/bin, /usr/local/bin).
  • People run it by name and do not need to know what language it is written in.
  • You can rewrite it later in Python or Go without changing how it is called.

Keep .sh when the file is read or sourced, not run:

  • It is a library that other scripts source, such as posit-lib.sh.
  • It lives in a repository next to other build or CI files (ci/test.sh), where the extension tells editors, linters, and readers what it is.

The scripts here follow that rule. posit-host, posit-workbench, posit-connect, posit-pm, and posit-groups are commands with no extension, installed with mode 0755. posit-lib.sh is sourced by each of them and installed with mode 0644, so it cannot be run by mistake.

The shebang

The extension says "shell", not which shell. The first line of the file (the shebang) decides that. Google requires #!/bin/bash for executables.2 These scripts follow it, so they always run the system Bash, whatever is first on the PATH.

Aliases, functions, and scripts

Bash gives you three ways to put a name on a command. Each one fits a different job.

Mechanism Good for Limitations
Alias Short names for a command, default flags Arguments can only be added at the end; no logic; not expanded in scripts
Shell function Logic, arguments, and changes to the current shell (cd, export) Only exists in the shell that defined it
Script on the PATH Anything reusable, called from other scripts, cron, or ssh host command Runs in its own process, so it cannot change the calling shell

When you are not sure which one to use, ask the questions below in order. The current shell comes first, because a yes there rules out a script.

flowchart TD
    Start(["New command"]) --> Shell{"Must it change<br/>the current shell?<br/>(cd, export)"}
    Shell -->|yes| Func("Shell function")
    Shell -->|no| Reuse{"Called from scripts,<br/>cron, or ssh?"}
    Reuse -->|yes| Script("Script on the PATH")
    Reuse -->|no| Short{"Only a short name<br/>or default flags?"}
    Short -->|yes| Alias("Alias")
    Short -->|no| Func

    style Func fill:#e8f1f8,stroke:#447099,stroke-width:2px,color:#1a1a1a
    style Script fill:#e8f1f8,stroke:#447099,stroke-width:2px,color:#1a1a1a
    style Alias fill:#e8f1f8,stroke:#447099,stroke-width:2px,color:#1a1a1a

Practices for aliases and functions

An alias is the right tool for adding flags (alias ll='ls -lah') or shortening a long command (alias k=kubectl). Bash has no way to use arguments inside an alias's replacement text. Its manual says that for almost every purpose, shell functions are preferable to aliases.4 Google's guide also says functions should always be preferred, and that aliases should be avoided in scripts.3

Practices that follow from this:

  • Put scripts on the PATH instead of aliasing them. Create ~/.local/bin (or ~/bin), add it to the PATH once, and copy scripts there. They then work from other scripts, from cron, and over ssh. Aliases do not.
  • Use a function when you need an argument anywhere but the end. alias mkcd='mkdir -p "$1" && cd "$1"' does not work; mkcd() { mkdir -p "$1" && cd "$1"; } does.
  • Use a function (or source a file) when the code must change your current shell. A script runs in a child process, so a cd or export inside it has no effect on the shell that started it.
  • Do not give an alias the name of a real command if it changes the command's behavior. alias rm='rm -i' feels safe until you work on a server without it. Use a new name (alias rmi='rm -i').
  • Keep aliases and functions in their own file, such as ~/.bash_aliases, and source it from ~/.bashrc. The file is then easy to keep in version control and copy to a new server.
  • Quote alias definitions with single quotes, so that variables and $(...) expand when the alias runs, not when it is defined.
  • Do not call aliases from scripts. Bash does not expand aliases in a shell that is not interactive unless the expand_aliases option is set.4
  • Run type name to see whether a name is an alias, a function, a builtin, or a file.

A starting setup

Add the lines below to ~/.bashrc. The case statement adds ~/.local/bin to the PATH only when it is not already there, and the last line sources your aliases file if it exists.

# ~/.bashrc
case ":$PATH:" in
    *":$HOME/.local/bin:"*) ;;
    *) export PATH="$HOME/.local/bin:$PATH" ;;
esac
[ -f ~/.bash_aliases ] && . ~/.bash_aliases

The aliases and functions themselves go in ~/.bash_aliases:

# ~/.bash_aliases
alias ll='ls -lah'
alias gs='git status'
mkcd() { mkdir -p "$1" && cd "$1"; }

Scripts go in ~/.local/bin with no extension, mode 0755, and a shebang on the first line. Short names for commands become aliases, and anything in between becomes a function. None of this needs root, so it works on a server where you do not hold sudo.