I want to add text to the end of the first line of a file using a bash script. The file has a /etc/cmdline.txt file that does not allow line breaks and requires new commands separated by a space, so the text I want to add should really be on the first line.
What I got so far:
line=' bcm2708.w1_gpio_pin=20'
file=/boot/cmdline.txt
if ! grep -q -x -F -e "$line" <"$file"; then
printf '%s' "$line\n" >>"$file"
fi
But this adds the text after breaking the line of the first line, so the result is incorrect. I need to either trim the contend file, add text and a line feed, or somehow just add it to the first line of the file without touching the others somehow, but my knowledge of bash scripts is not good enough to find a solution here, and all the examples that I find on the Internet add the beginning / end of each line in the file, not just the first line.
source
share