When a comment leads to a syntax error in Bash

When you are writing Bash scripts and commenting out commands which are performing arithmetic operations you must be aware that they’re still interpreted in particular cases.

Let’s assume we have the following code.

if [ "${_buildonly}" == "yes" ];
then
    :#make --jobs=$((1+get_amount_of_cpus));
fi

There’s nothing wrong with the example so far. The if statement will check the value of the _buildonly variable and if it’s yes a NOP is executed. A NOP command is also known as no operation and just does nothing. In Bash you need it as a placeholder if you leave the body of a loop or if statement empty.

As programmers are not perfect and do mistakes let’s remove a closing brace ‘)’ from the arithmetic expression.

if [ "${_buildonly}" == "yes" ];
then
    :#make --jobs=$((1+get_amount_of_cpus);
fi

If this code is now executed the interpreter will be unhappy with you and raises his voice.

./installer.sh: line 244: unexpected EOF while looking for matching `)'
./installer.sh: line 255: syntax error: unexpected end of file

The reason is that the arithmetic expression in the comment is still being interpreted. The error doesn’t appear in every case. It’s showing up especially when leading spaces are left out.

So keep this in mind, always add a leading space or put the comment on its own line.

This behaviour was detected in bash 4.2.39 and zsh 5.0.0 on a x86_64 system. Other versions might or might not be affected.

I would say this is a bug. Maybe this behaviour has already been there for a (long) while. If you know some good reason for that behaviour, feel free to leave a comment.

7 Comments

  1. Gunstick

    a comment always has to be preceded by whitespace, if your if stetement would be executed you will get

    :#make: command not found

    So your nop is not a nop, but a command (which does not exist) followed by it’s parameters which have to be interpreted.

    Now if you make the parameters wrong by missing the parenthesis, then the complete if statement can’t be parsed and gives error.

  2. slopjong

    In the mentioned versions the nop worked fine with no preceded whitespace as long as there was no mathematical expression in it.

    What bash/zsh version are you using?

  3. Gunstick

    No the nop did not work. Nothing to do with math or not. It’s not a nop in the first place.

    So let’s throw some test code that you should run.

    if  true 
    then
      :#nop code (with an error
    fi
    

    => bash: syntax error near unexpected token `(‘

    if  false 
    then
      :#nop code (with an error
    fi
    

    => bash: syntax error near unexpected token `(‘

    if  true 
    then
      :#nop code without error
    fi
    

    => :#nop: command not found

    if  false 
    then
      :#nop code without error
    fi
    
    # all ok   <= yes, because it does not even try to find command ":#nop" (the if is false)
    # this does not mean that this is a nop, it's still a "command not found"
    
    
    # same test with space comment:
    if  true 
    then
      : #nop space code (with error
    fi
    
    # all ok
    
    if  false 
    then
      : #nop space code (with error
    fi
    # all ok
    
    if  true 
    then
      : #nop space code without error
    fi
    # all ok
    
    if  false 
    then
      : #nop space code without error
    fi
    # all ok
    

    Conclusion: you are wrong, I am right :-)

  4. slopjong

    To make it clear. When I removed the math expression I didn’t get a “is not command” error message and the if statement worked fine in my case while testing the condition against false and true.

    There might be differences with other bash/zsh versions as posted in the article.

    Let’s do a deal. Once I show you in real life that my script works as long as there’s no math expression, you spend me a spicy pizza :-P

  5. In this time frame I have learned, lost, cried and felt elation, all at varying levels.

  6. Hello blogger, do you monetize your slopjong.de ? There is easy method to earn decent money every month,
    just search on youtube – How to earn with wordai 4

  7. They drive to the new destination. The unloading happens there, the receipt s signed and everyone goes house blissful.
    Something similar is applicable to worldwide household movers and corporations
    shifting internationally. They estimate the gap and determine which mode of transportation would swimsuit the
    state of affairs best the same manner through which professional
    moving companies do.

    Must be capable to work successfully below time constraints and frequent
    interruptions

    British Indian Ocean Territory

    Take pleasure in your new condominium

    Orders will not be issued till the FMRC process is accomplished

    American Movers and Storage Association

    Full Service Storage

    Donate the outdated stuff or hold a moving sale

    Transfer right into a Virtual Workplace and Let Your Workers Do business from home!
    Publisher: VH Worldwide Business Options Might getting a digital workplace save your business cash?
    Many businesses have found smart ways of reducing operating prices over the previous few years –
    they’ve needed to! One of the ways some companies have achieved this is by
    choosing to operate through a digital workplace instead of unnecessarily paying full-time
    rent for expensive office space.

    You can nearly certainly find a college and program here to
    swimsuit any academic needs, whether or not you are searching for a high quality training to
    your kids, to launch or additional your personal profession, or to carry in your lifelong
    studying journey. Publisher: andersonssmith Taxation in Canada is administrated by the CRA (Canada Income Company), which was earlier often called ‘Revenue Canada’ (Canada Customs and Revenue Company).

    In contrast with charges of taxation within the United States, Canadian taxations are larger.
    Writer: skymaggie1 Diamond drill bits from global mineral exploration company Boart Longyear are having a optimistic influence on iron-ore drilling operations within the distant
    area of Schefferville in northern Quebec, Canada.

    It’s time to begin making your checklist for shifting into a brand new home.
    From budgeting for the transfer to getting ready for life in your … Proceed reading “Moving Into a House for the first Time? Hoping to spruce up your own home in 2018? Fortuitously, you don’t should plan a major dwelling renovation to improve your abode. What Do I Have to Do On Transferring Day? As soon as all of the furnishings and packing containers are on the moving truck, snap just a few quick pictures of your now-empty condo as proof of the situation you left the area in. Before the moving truck arrives to unload your entire possessions, decide a corner or room the place containers that don’t must be opened instantly could be saved. These applications generally include 2 to 4 years of technical instruction and OJT. The Laborers’ Worldwide Union of North America (LIUNA) requires a combination of OJT and related classroom instruction in such areas as signaling, blueprint studying, using proper instruments and equipment, and following well being and security procedures. Laborers who remove hazardous materials (hazmat) should meet the federal and state necessities for hazardous supplies elimination workers. Relying on the work they do, laborers may have particular certifications, which may be attained via LIUNA. Rigging and scaffold constructing are generally attained certifications. Certification can assist staff prove that they’ve the knowledge to carry out extra advanced tasks. Because of this the proprietor is really useful to search for a very good company that can be able to offer the companies. Corporations selling these advanced bins use particular tools to hold them around. The tools are fabricated to ensure the tools are moved safely. Though, an individual can still purchase the gear and fabricate it if he or she is aware of about the fabrications. Some safes and vault are troublesome to maneuver round. They need particular therapy.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>