8

Is same question as How can I have a newline in a string in sh? but in fish shell.

In bash we have:

$'this is line\nthis is another line'

who produces the expected result, but in fish this doesnt works.

fish has a similar way to do this?

Edit 1:

has the literal method wisely mentioned by the faho:

'this is line this is another line'

But i'm really curious about the existence of a method keeping reference for \n as line break like in shell.

I want to know if I can use this string "this is a line\nthis is another line" making sure that the fish will consider \n as a line break and not a literal.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
Iury Fukuda
  • 109
  • 1
  • 5

3 Answers3

7
  1. Fish replaces \n outside of quotes with a newline, so you can stop and restart the quotes (note no "$" before the quotes):

'this is line'\n'this is another line'

  1. Fish follows quotes across lines, so you can include a literal newline:

'this is line this is another line'

faho
  • 14,470
  • 2
  • 37
  • 47
  • thanks for your time with this answer. i can use with this way in shell too. have another way to do this keeping '\n' like in shell? – Iury Fukuda Jan 24 '20 at 02:48
  • No, you can't do it this way in "shell". POSIX-like shells do not interpret `\n` outside of `$''` - `echo 'a'\n'b'` prints `anb`. – faho Jan 24 '20 at 16:27
  • There is also, in both fish and POSIX shells, `echo -e`, which will interpret backslash-escapes in strings, so `echo -e 'a\nb'` will also print a, a newline and b. I'd recommend just using the first way I gave you, as that's available regardless of what you're passing the argument to. – faho Jan 24 '20 at 16:28
3

You can use echo -e (Enable interpretation of backslash escapes), as pointed out by @faho in his comment:

$ echo -e "## Foo\nBar"
## Foo
Bar

From man echo:

Escape sequences

   If -e is used, the following sequences are recognized:

   o \ backslash

   o \a alert (BEL)

   o \b backspace

   o \c produce no further output

   o \e escape

   o \f form feed

   o \n new line

   o \r carriage return

   o \t horizontal tab

   o \v vertical tab

   o \0NNN byte with octal value NNN (1 to 3 digits)

   o \xHH byte with hexadecimal value HH (1 to 2 digits)
Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75
-2

Edit your config.fish file.
Example in Ubuntu sudo nano /etc/fish/config.fish
Prompt function is what controls the appearance of the fish terminal. Paste this code:

# Put system-wide fish configuration entries here
# or in .fish files in conf.d/
# Files in conf.d can be overridden by the user
# by files with the same name in $XDG_CONFIG_HOME/fish/conf.d

# This file is run by all fish instances.
# To include configuration only for login shells, use
# if status --is-login
#    ...
# end
# To include configuration only for interactive shells, use
# if status --is-interactive
#   ...
# end
#

function fish_prompt -d "Write out the prompt"
    # change output color
    set -U fish_color_command             '2cff8f' -o

    # Aliases
    alias cls='clear'

    # Prompt
    printf '%s@%s%s%s%s\n@>' (whoami) (hostname | cut -d . -f 1) \
                    (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
end
Ngatia Frankline
  • 2,897
  • 2
  • 20
  • 19