How can I avoid sed error on bash script?

231 Views Asked by At

I need to add text after the last line, but I'm getting this error.

How can I avoid this error keeping the same actions?

sed -i -e '$a\  containers:' -e '$a\  - name: container' -e '$a\    image: httpd' -e '$a\    ports:' -e '$a\    - containerPort: 80' /path/to/file"
                            ^-- SC2154: a is referenced but not assigned.
1

There are 1 best solutions below

5
On

You have a trailing double quote:

#                                                  Here --v
sed -i [...] -e '$a\    - containerPort: 80' /path/to/file"

This, along with ShellCheck's warning, indicates that the whole sed command is actually part of a double quoted string. You should read more of the context around where this command was found.

For example, it could actually be part of a larger construct like this:

ssh myhost "
  [...]
  sed -i [...] -e '$a\    - containerPort: 80' /path/to/file"

This would be a real bug that makes the command fail, so it should not be ignored. In this case you would escape the $s.