2

I want to append fastboot to the end of the existing value of the GRUB_CMDLINE_LINUX_DEFAULT variable in /etc/default/grub not manually, but by using bash script and echo command. The problem is that initial values of this variable are different on different machines so I don't know which searching algorithm to use to locate where the value of the variable ends. I want to be shown how to append fastboot directly to the existing value.

lakcic89
  • 23
  • 1
  • 3

1 Answers1

2

No idea how to that in pure bash but typically a search and replace would call either on awk or sed.

sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT.*/& fastboot/'  /etc/default/grub

-i : do in in-place replacement

Followed by the search and replace syntax s/REGEXP/REPLACEMENT/

^GRUB_CMDLINE_LINUX_DEFAULT.* : a REGular EXPression that matches on the line(s) starting ^ with GRUB_CMDLINE_LINUX_DEFAULT and any number of characters after that string: .*

& fastboot : the REPLACEMENT string where the unescaped & character references the whole matched portion of the pattern space (i.e. whatever the REGEXP matched,in this example: the whole line) and append fastboot

HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Thanks. I tried this and got ` sed: -e expression #1, char 32: unterminated `s' command` – lakcic89 Nov 29 '17 at 10:46
  • Of course, your shell will interpret the `&` as a background signal, edited accordingly by enclosing the `s/REGEXP/REPLACEMENT/` in ticks `'s/REGEXP/REPLACEMENT/'` – HBruijn Nov 29 '17 at 10:52
  • 1
    Dear HBruijn! Big thanks again for help. May I ask just for another assistance. How can I put fastboot inside the value. Now it is appended after the value. I mean now I get `VAR="quiet splash" fastboot` – lakcic89 Nov 29 '17 at 10:56
  • Match differently! Don't match the whole line but for instance only match on REGEXP `/^VAR="/` and REPLACE with `/&fastboot /` – HBruijn Nov 29 '17 at 11:13