Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ""Here strings" in bash changes the syntax color of whatever follows it?

Tags:

bash

vim

I am writing a bash script which uses here strings (<<<), please see example below. The script works fine and gives expected output, but the problem is that the (vim) editor syntax color is all messed up after the line where here string was used. Any clues why and also how I can fix it ?

enter image description here

As text:

# get all running screens
scrcmd=$(ps auxw|grep -i screen|grep -v grep|awk '{print $15}')
allscr=()
while read -r line; do
        allscr+=("$line")
done <<< $scrcmd

echo "got screens, now do something else"
like image 299
abhi Avatar asked Mar 07 '17 02:03

abhi


People also ask

What is here string in bash?

A here string can be considered as a stripped-down form of here document. It consists of nothing more than COMMAND <<<$WORD, where $WORD is expanded and fed to the stdin of COMMAND. Example 17-13. Prepending a line to a file. #!/bin/bash # prepend.sh: Add text at beginning of file.

Does bash have syntax highlighting?

Bash Line Editor―a full-featured line editor written in pure Bash! Syntax highlighting, auto suggestions, vim modes, etc.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

The bash (really sh) highlight mode in Vim is multipurpose; it tries to cover POSIX sh, bash, and ksh. You have to tell it you specifically want bash.

:let b:is_bash=1
:set ft=sh

It should highlight properly after that.

If you only ever care about bash, you could just make this your default in .vimrc:

let g:is_bash=1

EDIT: As pointed out by Charles Duffy in comments, if you use a #! line, e.g.

#!/bin/bash

#!/usr/bin/env bash

Then vim should do the right thing on its own. That is probably easier, unless you have some reason you prefer not use #! lines.

(Even though that's likely easier, I am leaving my answer here because in this case you do not have a #! line, and that is not uncommon, especially in library code or files expected to be sourced, not executed.)

like image 79
Dan Lowe Avatar answered Oct 01 '22 02:10

Dan Lowe