I have an automatic setup script that is executed when a new user is created. It runs this line of code to set up the golang environment:
echo "export PATH="$PATH:$GOPATH/bin"" >> ~/.profile
echo 'export PATH="$PATH:$GOPATH/bin"' >> ~/.profile
Single-quoted strings in POSIX-like shells (such as
bash) treat their content as literals, which is what you want here.
The only reason to use a double-quoted string here would be to selectively expand variable references up front - which doesn't apply in your case.
That said, here's an example:
$ echo "Honey, I'm \"$USER\" and I'm \$HOME." Honey, I'm "jdoe" and I'm $HOME.
Backslash-escaping is used to escape embedded
$ instances that should be treated as literals.
As for what you tried:
is actually a string concatentation, composed of 3 separate strings:
"export PATH=", which, as a double-quoted string that happens not to contains
$-prefixed interpolation elements, expands to literal
$PATH:$GOPATH/bin, which, as an unquoted string, is subject to additional shell expansions, which not only involves expanding variables
$GOPATHto their respective values, but also applies word-splitting and pathname expansion (globbing).
"", which amounts to the empty string and is effectively ignored.
Note how POSIX-like shells allow you to compose larger strings (concatenate strings) by placing strings - unquoted or single-quoted or double-quoted - directly next to one another.