Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiline command in 'script:' with YAML?

I have a repository that uses Travis CI, and in the .travis.yml there I have this line:

script: - vim -Nu <(cat <<-EOF   set nocompatible |   filetype off   EOF   ) -c 'Script' > /dev/null 

Sadly this doesn't work, as this is transformed into a single line and is executed like this:

vim -Nu <(cat <<-EOF set no compatible | filetype off | EOF ) -c 'Script' > /dev/null 

This makes the EOF tag not working, as EOF needs to be in a single line. An alternative would be to just use normal quotes like this:

script: - vim -Nu <(cat 'set nocompatible |   filetype off   ) -c 'Script' > /dev/null 

Which works, and is fine, but I feel there must be a way to insert newlines into a .travis.yml. I have an alternative now, but I may not in the future. So how do you do it?

like image 992
hgiesel Avatar asked Aug 03 '16 13:08

hgiesel


People also ask

How do you write a multi line command in a shell script?

Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.

What does pipe do in YAML?

The pipe is used when you want newlines to be kept as newlines.


1 Answers

In YAML you can specify newlines in a scalar by using "" quoting and escaping the newlines (\n), or, and that is more natural for your case, by using a literal style block scalar:

script: - |   vim -Nu <(cat <<-EOF   set nocompatible |   filetype off   EOF   ) -c 'Script' > /dev/null 

This is a scalar starting with a line with a | (pipe symbol), followed by multiple lines for which the line-breaks are preserved.

  • The lines are normally indented (exception: a single top-level literal style block scalar).
  • After the | there can be modifiers: 1-9, used when your first line starts with spaces; +, - to influence stripping of final newlines (normally collapsed into one).
like image 187
Anthon Avatar answered Oct 11 '22 14:10

Anthon