rcheng
rcheng

Reputation: 61

Bash - How are '\' and newline characters interpreted within single quotes?

According to the GNU documentation here all text within single quotes should be interpreted literally. I then tried creating two aliases:

alias alias1='
    echo hello'

alias alias2='\
    echo hello'

Executing alias1 prints hello, as I expect. Executing alias2 results in no text being printed. Going into a terminal and manually entering \, enter, echo hello also prints hello. Shouldn't alias2 be identical to my manual test case?

Upvotes: 1

Views: 237

Answers (1)

chepner
chepner

Reputation: 531225

This may be related to a bug fix in bash 4.2. From the change log:

This document details the changes between this version, bash-4.2-alpha, and the previous version, bash-4.1-release.

  1. Changes to Bash

    a. Fixed a bug in the parser when processing alias expansions containing quoted newlines.

As far as the definitions go, alias1 should start with a newline, followed by several spaces, then the text echo hello. alias2 should be nearly identical, with the exception that it does not begin with a newline. Either way, the whitespace preceding echo is discarded after the alias expansion, during parsing.

Upvotes: 1

Related Questions