Shell scripts should avoid use of aliases

Do not use shell aliases in non-interactive shell scripts to stay shell-agnostic.

In Bash, aliases are not expanded in non-interactive shells unless the script first invokes

shopt -s expand_aliases

In Zsh, aliases are expanded in non-interactive scripts as well. However, while on macOS the default interactive shell is Zsh, non-interactive scripts with the usual #!/bin/sh shebang run Apple’s Bash in POSIX mode instead of Zsh.

List the aliases defined in the current shell (Linux, macOS, BSD) with:

alias

Our practice is to avoid aliases in non-interactive scripts and use explicit paths or functions instead. For example, to pin a user-specified GCC for CMake, pass the compiler paths directly:

#!/bin/sh

prefix="$HOME/.local/bin"
gcc_version=17
CC="$prefix/gcc-$gcc_version"
CXX="$prefix/g++-$gcc_version"
FC="$prefix/gfortran-$gcc_version"

cmake -B build -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX -DCMAKE_Fortran_COMPILER=$FC

cmake --build build

Windows PowerShell

Although the preceding discussion is for Unix-like systems, Windows PowerShell can also use aliases and has similar considerations for non-interactive scripts.

To list the aliases defined in the current PowerShell session:

Get-Alias

An analogous script for non-interactive PowerShell scripts would be like:

$prefix = "$HOME/.local/bin"
$gcc_version = 17
$CC = "$prefix/gcc-$gcc_version"
$CXX = "$prefix/g++-$gcc_version"
$FC = "$prefix/gfortran-$gcc_version"

cmake -B build -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX -DCMAKE_Fortran_COMPILER=$FC

cmake --build build