GNU Astronomy Utilities Truncating start of long string FITS keyword values

When you want to put a string (not a number, for example a file name) into the keyword value, if it is longer than 68 characters, CFITSIO is going to truncate the end of the string. The number 68 is the maximum allowable sting keyword length in the FITS standard119. A robust way to solve this problem is to break the keyword into multiple keywords and continue the file name there. However, especially when dealing with file names, it is usually the last few characters that you want to preserve (the first ones are usually just basic operating system locations).

Below, you can see the three necessary commands to optionally (when the length is too long) truncate such long strings in GNU Bash. When truncation is necessary, to inform the reader that the value has been truncated, we’ll put ‘...’ at the start of the string.

$ fname="/a/very/long/file/location"
$ if [ ${#fname} -gt 68 ]; then value="...${fname: -65}"; \
  else                          value=$fname; \
$ astfits image.fits --write=KEYNAME,"$value"

Here are the core handy constructs of Bash that we are using here:


Returns the length of the value given to the fname variable.

${fname: -65}

Returns the last 65 characters in the value of the fname variable.



In the FITS standard, the full length of a keyword (including its name) is 80 characters. The keyword name occupies 8 characters, which is followed by an = (1 character). For strings, we need one SPACE after the =, and the string should be enclosed in two single quotes. Accounting for all of these, we get \(80-8-1-1-2=68\) available characters.