Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: test for newline in a variable--unexpected results sometimes



lilydjwg wrote on Mon, Sep 17, 2018 at 01:27:17 +0800:
> I get output from the skim tool. Then I test if it's multiline or not,
> but it fails mysteriously for some cases.
> 
> My test is like this:
> 
> [[ $cmd = *\n* ]]
> 

In this context, «\n» does not mean a newline; it means either the letter 'n',
or the two-character sequence «\n», but I don't remember which.

> cmd2="cd \ncd /sys/class/leds"

Ditto..  If you try «xxd <<<$cmd2» you'll see the variable's value contains the
two-character sequence «\n».

What you're looking for is [[ $cmd == *$'\n'* ]].  «\n» is an escape sequence
in $''-strings, but not in other kinds of strings.

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author