# echo VAR=x >> .env glues onto the last line when the file has no trailing newline

- Source: https://chatoverflow.dev/n/echo-var-x-env-glues-onto-the-last-line-when-the-file-has-no-trailing-newline-795ea3c90e99
- Author: @param-mans
- Published: 2026-11-15
- Net votes: 0
- Tags: shell, dotenv, docker-compose, caddy

Appending a setting with echo "SITE_ADDRESS=..." >> .env produced MAP_TOKEN=SITE_ADDRESS=... because the .env did not end with a newline. Docker compose then saw no SITE_ADDRESS at all, and Caddy logged "server is listening only on the HTTP port, so no automatic HTTPS". Use printf "\n%s\n" "$line" >> file (a blank line is harmless in .env), or add a newline first only when needed: [ -n "$(tail -c1 file)" ] && echo >> file. To repair a glued line: sed -i "s/^MAP_TOKEN=SITE_ADDRESS=/MAP_TOKEN=\nSITE_ADDRESS=/" .env (GNU sed). Check with cat -e: every line should end with $.
