0

Couldn't find any answer for this problem, so here I am.

I'm using ncatted from NCO inside a bash script to dinamycally change some variables unit's names in a netcdf.

ncatted is called like so (note the double quotes around Celsius):

ncatted -O -a units,temp2m,o,c,"Celsius" ncfile.nc

In order to replicate the last command in my bash script:

variables=(temp2m u10)
units=(Celsius Knots)
echo " Setting up new units"
for k in ${!variables[@]};
do
   ncatted -O -a units,${variables[$k]},o,c,'"'${units[$k]}'"' ncfile.nc
   echo ncatted -O -a units,${variables[$k]},o,c,"'"${units[$k]}"'" ncfile.nc

   # prints ncatted -O -a units,temp2m,o,c,"Celsius" file.nc
   # prints ncatted -O -a units,u10,o,c,"Knots" file.nc
done

While printing the command to shell looks ok, inside the netcdf, the new attribute will always appear like this;

units: \"Celsius\"

I've tried multiple variations of single quotes, double quotes and backslashes, but I always end up with the same result.

Although its not the end of the world, its really annoying, so I was hoping to know if anyone has a good explanation for it.

Thanks

Elcook
  • 145
  • 1
  • 10
  • You are confused about the meaning of quotes. If you pass in literal quotes, then yes, you are passing in literal quotes. If you don't want to pass in literal quotes, don't. This is probably a duplicate. The meaning of quotes in the shell is to pass in a string verbatim; by the time your original command runs, there are no quotes in the argument list which `ncatted` receives. – tripleee Apr 28 '21 at 11:03
  • Thanks for taking the time. I'm not sure I understood your answer. ncatted needs to have the new name between double quotes or it won't work and thats why I know my command in bash is correct. What is confusing me is why its adding the \" and \" into the name. Unless you are telling me I messed up my command inside the loop – Elcook Apr 28 '21 at 14:11
  • If those quotes were really genuinely necessary, the correct command line in the shell wold look like `ncatted -O -a 'units,temp2m,o,c,"Celsius"' ncfile.nc` (or some equivalent reformulation, like `ncatted -O -a "units,temp2m,o,c,\"Celsius\"" ncfile.nc` etc). Generally, whatever works on the command line will work exactly verbatim in a script, too. – tripleee Apr 28 '21 at 14:42
  • ok, I think I see the confusion. I may not have phrased my question right (or my original command). The command line in the shell only works like so `ncatted -O -a units,temp2m,o,c,"Celsius" ncfile.nc`, and will edit the attribute as it is suposed to. Only when I try to write the same command in bash, is when I get the name of the attribute as \"Celsius\". I've edit my original code for clarification – Elcook Apr 28 '21 at 15:43
  • In shell syntax, double-quoting changes how certain metacharacters (like spaces, `*`, etc) within the double-quoted section are treated. But `Celsius` has no metacharacters that're affected by double-quoting, so the quotes in your command have no effect (and the shell removes them before passing them to the `ncatted` command, so no effect there either). Try your original command without the double-quotes -- it should do the exact same thing as with them. If it does something different, we'll need to figure out why (and how?!?!?!?) before going any further. – Gordon Davisson Apr 28 '21 at 17:12
  • Your edit just changed one incorrect version of quoting for another. The simple and obvious fix is `ncatted -O -a units,"${variables[$k]}",o,c,"${units[$k]}" ncfile.nc` which in fact is equivalent to `ncatted -O -a units,${variables[$k]},o,c,${units[$k]} ncfile.nc` if the variables' values don't require quoting, like in your example (if they did, you would also need to quote the values when you initialize the arrays). – tripleee Apr 28 '21 at 17:13
  • Ok, I read a bit more about double quotes on bash and I was indeed confused about all of this. In my code I, as you both said, don't need them at all for it to work. Ncatted's manual got me thinking I absolutly needed those double quotes – Elcook Apr 29 '21 at 17:04

0 Answers0