3

I am trying to write a while loop within one while loop in bash , the syntax of the second while doesn't get highlighted . Is there any special syntax for using this ?

while [ "ka" = $name ] 
do 
  while [ "ka" = $name ] //this while is not highlighted
  do
  done
done
Jens
  • 69,818
  • 15
  • 125
  • 179
K_TGTK
  • 765
  • 1
  • 8
  • 24

3 Answers3

3

As other answers have already pointed out, this is a bug in the syntax plugin that affects non-Bash shell syntax.

Please submit a bug report (basically a link to this page) to the script's author, Charles Campbell; his email address is in the script's header; the script is located at syntax/sh.vim in the Vim install directory.

Should you not get a response, please inform the vim_dev mailing list (cp. http://www.vim.org/community.php; you need to register first) about this; hopefully, someone else will pick it up.

workaround

As a workaround, you could force Bash syntax by default (in your ~/.vimrc):

:let g:is_bash      = 1
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I was able to see the issue when I converted my file to use #!/bin/sh at the top as l0b0 pointed out. And yes let g:is_bash = 1 also fixes the issue. – cforbish Jun 05 '13 at 04:33
2

You're right. For some reason it looks like VIm 7.3 only highlights nested while commands if the shebang line points to bash. For example, files starting with any of these are highlighted correctly:

#!/usr/bin/env bash
#!/usr/bin/bash
#!/bin/bash

But not without a shebang line or with the following lines:

#!/bin/sh
#!/usr/bin/env sh

Test file:

[shebang line]
while true
do
    while true
    do
        while true
        do
            :
        done
    done
done

:set filetype? prints filetype=sh in all cases, so it looks like it's a bug.

Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223
1

Try the following script. It works for me

#!/bin/bash
i="1"
while [ $i -lt 4 ]
do
        j="1"
        while [ $j -lt 4 ]
        do
                echo $i
                j=$[$j+1]
        done
        i=$[$i+1]
done

It's output is

1
1
1
2
2
2
3
3
3

As expected.

The problem might be the code prior to the one you posted here, since I can't really find a syntax error in your code.

itzhaki
  • 822
  • 2
  • 7
  • 19