I'm new to GRASS GIS and programming and I'm running into a lot of what I imagine are beginners problems and something similar to this has been asked before, but not In a way that I understand.

I'm trying to bash import tif. files and tried (from GRASS GIS 6 tutorial https://grasswiki.osgeo.org/wiki/GRASS_6_Tutorial/Raster_data_management):

for file in `ls *.tif`
  r.in.gdal input=$file output=${file%.tif}

but I get

File "<input>", line 1
    for file in `ls *.tif`
SyntaxError: invalid syntax

Since I'm new to programming world I have no idea how to fix this.

2 Answers 2


The answer above, in accordance with the title of your question, refers to scripting in a bash shell. (Not python). If you would like more information on bash scripting, have a look at this tutorial (referred from the GRASS bash scripting wiki page

If you want to work in python, then the language is quite different. For example, to loop thru a directory of *.tif files in python might go:

import os,glob
for tif_file in glob.glob("*.tif"):
    new_rast = os.path.splitext(tif_file)[0]  
    grass.run_command('r.in.gdal', input=tif_file, output=new_rast)

So, first choose your tools :-)

  • ok, thanks both of you, very helpful answers for a beginner, helped me towards a better understanding of these programming languages. I'm trying to use python for now, trying to figure all this out.
    – Adam H
    Oct 8, 2014 at 8:55

The proper syntax is:

for file in $(ls *.tif); do r.in.gdal input=$file output=${file%.tif}; done;

Where you gather the individual values provided by ls and iterate through them, so you have to put a variable sign($) before the ls command and put it between brackets.

When you call for the file variable later, you can either call for $file as in the input parameter or call a function to it, like the stripping function with the braces. If you call it in a function, you have to put a semicolon after, because in bash, the semicolon signs the end of a function/command.

  • I still get an syntax error, only know it points at the $ sign.
    – Adam H
    Oct 7, 2014 at 12:20
  • What is the full code, you're getting the syntax error for? For me, typing the code above in the GRASS terminal works fine. Oct 7, 2014 at 14:02
  • Well, i'm still trying to figure out what to put in the code, I haven't figured out what basic thing to put in a code, like what would precede this syntax. I just put the syntax in the python terminal in grass.
    – Adam H
    Oct 7, 2014 at 14:14
  • The proper syntax, for the first part of the loop command is: for file in *.tif. The ls command is neither needed nor correct, I think. For more read gnu.org/software/bash/manual/html_node/…. Oct 16, 2018 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.