0

I need to export a password that is like this

export PASSWORD='vfR"&aDA'GzV3(Yg'

but above does not work when i do below three combinations with backslash.

export PASSWORD='vfR"&aDA\'GzV3(Yg'
export PASSWORD='vfR\"&aDA\'GzV3(Yg'
export PASSWORD='vfR\"&aDA\'GzV3\(Yg'
Robin Green
  • 30,802
  • 16
  • 100
  • 180
  • 1
    Are the single `'` at the start and end part of PASSWORD? – Gino Mempin May 25 '18 at 00:55
  • 3
    @jww Please stop flagging shell questions as off topic. Despite the overlap with other Stack Exchange sites shell scripting is on topic here. – John Kugelman May 25 '18 at 03:07
  • The shell *is* a programming language; the fact that it is extensively used from a REPL and is optimized for running other programs doesn't change that. – chepner May 25 '18 at 03:25

2 Answers2

1

Try export PASSWORD="vfR\"&aDA'GzV3(Yg" I think the single quotes are preventing the escaping slash from doing what you expect. So, try double quotes. You can view the answer to a question on single versus double quotes here.

Brandon Miller
  • 4,237
  • 1
  • 16
  • 24
1

In single-quotes, escapes don't work; they're just a backslash character. The only thing that has a special meaning after a single-quote is another single-quote, and that always marks the end of the single-quoted section. As a result, you cannot embed a single-quote in a single-quoted string.

You have several options. You can end the single-quoted section of the string and use something else to protect the single-quote:

# Single-quoted string, escaped single-quote, then another single-quoted string:
export PASSWORD='vfR"&aDA'\''GzV3(Yg'
# Single-quoted string, double-quoted single-quote, then another single-quoted string:
export PASSWORD='vfR"&aDA'"'"'GzV3(Yg'

Or use a double-quoted string instead (and then escape any double-quotes, backslashes, dollar signs, backquotes, etc that you want in the string):

export PASSWORD="vfR\"&aDA'GzV3(Yg"

Or you could skip quotes entirely and just escape everything individually:

export PASSWORD=vfR\"\&aDA\'GzV3\(Yg
Gordon Davisson
  • 107,068
  • 14
  • 108
  • 138