I'm having trouble inputting random generated passwords into some scripts.
I generate the password, and paste it between "". But sometimes it contains characters that sends the whole thing crashing down, like " # or whatever.
EDIT: I have been pointed to Quoting for a solution
There are several options for Quoting ( using \
, " "
and ' '
) but each require checking the string for special characters, like single quotes in a single quote block.
Is there a way to really paste a string without needing to check it or is doing that check always necessary when your random string can contain the ' character at least.
You can use a Here document:
The delimiter (here “EOF”) has to occur on a single line to end the here document and it needs to be quoted in the first line, for example
gives you the literal:
To use the string in a script, adjust it to read from stdin and replace
cat
with the script’s path in the example above.Note that the occurence of the delimiter string inside the password is not a problem, as the delimiter has to be alone on the line. The only way this does not work is if the delimiter matches exactly the password string, and as most passwords are created with certain requirements (e.g. “at least one number”) it should be easy to choose a delimiter that simply doesn’t meet one of the requirements. One could use “123456”, with that password failure is expected.