Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing bash script in vim, <<< brackets cause colour corruption

Tags:

bash

vim

vi

I have inherited several bash scripts that uses the following syntax to read from a string into an array:

read -a arr <<<$line

However, this causes the colour formatting in vim to break. Can anybody suggest a quick fix?

Update:

Ignore the content of the script, but note the colour variation after the '<<<' characters (i.e. echo statements are in purple):

enter image description here

like image 741
Steve Walsh Avatar asked Jun 01 '26 13:06

Steve Walsh


1 Answers

It happens if the #!/bin/bash shebang line is missing. Vim interprets the script as plain sh instead of bash. <<< is a bash-ism.

From sh.syntax:

" trying to answer the question: which shell is /bin/sh, really?
" If the user has not specified any of g:is_kornshell, g:is_bash, g:is_posix, g:is_sh, then guess.
if !exists("g:is_kornshell") && !exists("g:is_bash") && !exists("g:is_posix") && !exists("g:is_sh")
 let s:shell = ""
 if executable("/bin/sh")
  let s:shell = resolve("/bin/sh")
 elseif executable("/usr/bin/sh")
  let s:shell = resolve("/usr/bin/sh")
 endif
 if     s:shell =~ 'bash$'
  let g:is_bash= 1
 elseif s:shell =~ 'ksh$'
  let g:is_kornshell = 1
 elseif s:shell =~ 'dash$'
  let g:is_posix = 1
 endif
 unlet s:shell
endif

and

" Here Strings: {{{1
" =============
" available for: bash; ksh (really should be ksh93 only) but not if its a posix
if exists("b:is_bash") || (exists("b:is_kornshell") && !exists("g:is_posix"))
 syn match shRedir "<<<"        skipwhite       nextgroup=shCmdParenRegion
endif
like image 139
John Kugelman Avatar answered Jun 04 '26 12:06

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!