简体   繁体   中英

Show multiline aliases in zsh with line breaks as in bash

When printing all available aliases on stdout via command alias a multiline alias in bash will printed with the line breaks but it does not in zsh.

Example with the identical multiline alias definition for bash .bash_aliases and zsh .zsh_aliases :

alias hello='
        echo "Hello"
        echo "beautiful"
        echo "world"'

When the alias hello is executed the result is the same on both shells.

But when comparing the printout of the definiton via alias hello on stdout...

  • in bash the output is:
alias hello='
        echo "Hello"
        echo "beautiful"
        echo "world"'
  • whereas in zsh the output looks like this:
hello=$'\n        echo "Hello"\n        echo "beautiful"\n        echo "world"'

Why is the \n in zsh not printed as new line and the tabulator \t not respected like in bash`s stdout?

I tried several escaping but without success.

You can use the print built-in to display the text with newlines:

print "$(alias)"

eg

> print "$(alias hello)"
hello=$'
        echo "Hello"
        echo "beautiful"
        echo "world"'

It's usually preferable to use a function for anything with multiple lines rather than an alias. With a function you have fewer worries about quoting and a generally easier-to-follow syntax:

hello2() {
  print Hello
  print beautiful
  print world
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM