def _maybe_add_password(command, password):
    """
    Add sudo password to command if required. Else NOOP.
    in: sudo apt-get install
    out: echo 'password' | sudo -S apt-get install
    """
    if not password or 'sudo' not in command:  # or 'sudo -S' in command:
        return command

    # Handle commands that are chained with &&
    blocks = command.split('&&')

    def fix_block(block):
        """Adds sudo and password where needed"""
        if 'sudo' in block and 'sudo -S' not in block:
            # Split the command string into a list of words
            words = block.split()

            for i, word in enumerate(words):
                if word == 'sudo':
                    words.insert(i + 1, '-S')
                    break

            words.insert(0, "echo '%s' |" % password)

            return ' '.join(words)

        return block

    fixed_blocks = [fix_block(block) for block in blocks]

    return '&&'.join(fixed_blocks)