skip to Main Content

I am running the following command:

echo 'my!#%^password' | sudo -S bash -c 'useradd -m -s /usr/sbin/nologin -p $(echo my!#%^password | openssl passwd -1 -stdin) george'

The issue I am having is the special characters. Some break the command, like !@& which seem to be the issue. Is there a way to run the same command to allow any special characters?

2

Answers


  1. Chosen as BEST ANSWER

    When creating Linux passwords, the following non-alphanumeric characters need to be escaped in the shell because they have special meanings:

    • ! (exclamation mark): In many shells, this is used for history substitution.
    • $ (dollar sign): This is used to denote variables.
    • & (ampersand): This is used to run a command in the background.
    • * (asterisk): This is used as a wildcard for file matching.
    • ? (question mark): This is also used as a wildcard for file matching.
    • " (double quote): This is used to define strings, and within them variable and command substitution is performed.
    • ' (single quote): This is used to define strings, and within them no substitution is performed.
    • (backslash): This is used to escape characters that have special meaning.
    • | (pipe): This is used to pipe the output of one command to another.
    • ; (semicolon): This is used to separate multiple commands.
    • (, ) (parentheses): These are used to create subshells.
    • <, > (less than, greater than): These are used for input and output redirection.
    • {, } (curly braces): These are used in parameter expansion and for executing commands in the current shell context.
    • [, ] (square brackets): These are used in pattern matching and array indexing.
    • # (hash): This is used for comments.
    • ~ (tilde): This is used for home directory substitution.
    • ^ (caret): In some shells, this is used for history substitution or pattern removal.
    • (space): This is used to separate items and commands.
    • n (newline), r (carriage return), t (tab): These are whitespace characters that can cause issues in passwords.

    Remember, it's not that you can't use these characters in passwords, but if you're entering a password in the shell (for example, in a script), you would need to escape these characters to prevent them from being interpreted by the shell.


  2. You can "escape" those special characters by using backslash. This character:

    Those characters that are breaking the command need to be preceded by a backslash. Otherwise they are interpreted as Regular Expression characters.

    e.g.

    echo my!#%^password
    becomes
    echo my!#%^password
    

    I might have missed some but the CLI output should tell you if you’ve missed them.

    Edit: apparently the user interface for stack overflow interprets those backslash characters and removes them. I’ve surrounded the example in code format.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search