Firefox uses a text file named ~/.mozilla/firefox/profiles.ini to hold a list of profiles. The entries look similar to this:




I need to add a new entry to profiles.ini using a bash script. The catch is that the profiles need to be numbered sequentially and I do not know in advance how many profiles each user has. In the above example, I would need to add [Profile9]. If instead, I add [Profile8] or [Profile10] or any other number, it will not work correctly.

How can my script figure out which is the highest profile number currently in use, then increment that and append a new profile to profiles.ini?

I have gotten as far as something like this used in a for-loop, but I don't know how to get $NewNumber.

echo "[Profile$NewNumber]
Path=NewPath" >> /home/$myuser/.mozilla/firefox/profiles.ini

Perhaps use awk to extract the "[Profile]" lines then process away the "[Profile ... ]" bits, then sort the results numerically, keeping only the last (highest) one:

highest=$(awk '/^\[Profile[0-9]+\]$/ { s=substr($0, 9); sub("]","", s); print s}' < /home/$myuser/.mozilla/firefox/profiles.ini |sort -n | tail -1)
highest=$((highest + 1))
printf "[Profile%d]
Path=NewPath" "$highest" >> /home/$myuser/.mozilla/firefox/profiles.ini

Why not an all awk approach?

awk '
/Profile[0-9]+/ {PRNR = $0
                 gsub (/[^0-9]/, "", PRNR)
END             {print ""
                 print "[Profile" ++PRNR "]"
                 print "Name=NewProfile"
                 print "IsRelative=1"
                 print "Path=NewPath >> /home/$myuser/.mozilla/firefox/profiles.ini"
' profiles.ini
  • I like this approach. It is returning the correct profile number. However, running it is outputting the new profile to stdout rather than appending it to /home/$myuser/.mozilla/firefox/profiles.ini. I assume also that the final profiles.ini refers to the input file, right? – MountainX Oct 28 '18 at 21:00
  • Redirect stdout to a temp file, and then overwrite the original. And yes, profile.ini is the original input file. – RudiC Oct 28 '18 at 22:13
  • Redirecting stdout does work, but that last bit, >> /home/$myuser/.mozilla/firefox/profiles.ini, ends up in the output file. Somehow, the >> isn't doing it's intended job. I could delete it and redirect stdout myself, but that does not seem to be what you intended in your answer. If that's resolved, I could accept your answer because I do like the all-awk approach. – MountainX Oct 29 '18 at 0:02
  • Don't append to the original. awk ... >/tmp/TMP$$; mv /tmp/TMP$$ original_file will install the new, extended .ini file --- Ohh, got you. remove that >> /home.../profile.ini from the awk script... – RudiC Oct 29 '18 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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