Use grep to check if the first line exists if it does not add it to the end of the file using the double pipe operator || . The operation will be short if the first command returns true, and therefore the second command will only be executed when the first failure.
grep -q '^option.*' file.txt || echo 'option newparam' >> file.txt
The -q suppresses grep output, and the || only executes the echo command if the grep fails, that is, if $? not 0 . A simple example:
$ cat file.txt line 1 line 2 line 3 $ grep -q 'line 4' file.txt || echo 'line not in file' line not in file
Special variable $? contains the exit value from the previous command.
$ grep -q 'line 4' file.txt $ echo $? 1 $ grep -q 'line 3' file.txt $ echo $? 0
source share