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_aliasesIn 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:
aliasOur 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 buildWindows 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-AliasAn 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