Skip to content

Files

Latest commit

 

History

History
31 lines (21 loc) · 871 Bytes

SC2263.md

File metadata and controls

31 lines (21 loc) · 871 Bytes

Pattern: Use of alias with definition

Issue: -

Description

Alias expansion happens at parse time, which means to have an effect, the alias command must be executed not just before the alias is invoked, but before the invocation is parsed.

A shell will parse commands until it has a complete set of commands followed by a linefeed. This includes compound commands like { brace; groups; } and while loops; do true; done.

Example of incorrect code:

function checksum() {
  type md5 && alias md5sum=md5
  md5sum "$@"  # This calls `md5sum`, not `md5`
}

Example of correct code:

function checksum() {
  type md5 && md5sum() { md5sum "$@"; }
  md5sum "$@"  # Now this would call `md5` when applicable
}

Further Reading