I learnt how to use GRASS GIS from an Ubuntu machine, directly using the command line. I wrote my commands in a text editor (e.g., Notepad++), and I just copied and pasted the lines I was interested into (without using the GUI or loading a script) like this:

g.region -p raster=EU_3035 res=1000 

while read sp;
echo "LOAD $sp"
r.in.gdal --o input='/data/lisa/GRASS/'$sp'.tif' output=$sp
done < /data/lisa/GRASS/species.txt

Now I had to switch to use GRASS GIS on my Windows 10 machine. If I try to paste something like that (various lines with different commands), g.region and while read are interpreted as a single command, even if I separate them with a ;. Of course I can run each command separatedly, first

g.region -p raster=EU_3035 res=1000 

and then

while read sp;
echo "LOAD $sp"
r.in.gdal --o input='C:\Users\Lisa\Desktop\PhD\GRASS\'$sp'.tif' output=$sp
done < C:\Users\Lisa\Desktop\PhD\GRASS\species.txt

but I want to run everything together, and in any case, I get this error:

'while' is not recognized as an internal or external command, operable program or batch file.

I can understand the same language cannot be used between Linux and Windows. Unfortunately, I don't know any Python. Is there a way to copy-paste my commands as I used to, or is there a way to write them all in a script and load it? I already tried to save my commands (g.region and while read, in this case) into a script (script1.sh) that starts with #!/bin/bash, and then I tried to write both into the command line and into the GUI console sh C:\Users\Lisa\Desktop\PhD\GRASS\script1.sh, but again I get the same error as before. Here Return variable with GRASS GIS on a Windows machine a "unix like shell" is mentioned. Is that something that can allow me to use GRASS as if I was in a Linux environment, even if I'm on Windows?

I am very inexperienced as you can see, so forgive any inaccuracy.

My GRASS GIS version is 7.8.7

1 Answer 1


When switching operating systems, you have to take into account that some existing workflows won't transfer over. For Windows, you'll have to take the time to learn the syntax for cmd.exe batch files, and convert your existing shell scripts. There are plenty of GRASS users on Windows, and if you post again on the grass-users maillist, someone will surely offer some suggestions.

But since you're switching, and you have to rewrite your scripts anyway, maybe now is the best time to learn python. Since python is pretty much cross platform, making the effort now will allow you to switch back and forth in the future with minimum effort.

Just as an example, here's what your above script might look like (not tested):

import os
import grass.script as gscript

# Read in species.txt file
home_dir = os.path.expanduser("~") # should be "C:/Users/Lisa" on your machine
data_dir = "Desktop/PhD/GRASS"
sp_file = os.path.join(home_dir, data_dir, "species.txt")
sp = sp_file.open().read().split("\n") # Should be a python list of species

# Build list of full paths to the tif files
sp_tifs = [os.path.join(home_dir, data_dir, x+".tif") for x in sp]

gscript.run_command('g.region', raster='EU_3035',  res=1000) 

# Now loop to load all rasters
for i in range(len(sp)):
        input=sp_tifs[i], output=sp[i], overwrite=True)

That should more or less work in Linux, Mac or Windows, with maybe minor changes to the paths.

  • Thanks, Micha. I think it is time to learn Python :)
    – LT17
    Commented Oct 13, 2022 at 8:55

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.