def test_grep_to_cmdrun_special_chars(runtime_cls, run_as_openhands, temp_dir):
    """Test patterns with special shell characters."""
    runtime, config = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
    try:
        # Create test directory and files with special pattern content
        setup_cmd = """
        mkdir -p /workspace/test_special_patterns && \
        echo "testing x && y || z pattern" > /workspace/test_special_patterns/logical.txt && \
        echo "function() { return x; }" > /workspace/test_special_patterns/function.txt && \
        echo "using \\$variable here" > /workspace/test_special_patterns/dollar.txt && \
        echo "using \\`backticks\\` here" > /workspace/test_special_patterns/backticks.txt && \
        echo "line with \\n newline chars" > /workspace/test_special_patterns/newline.txt && \
        echo "matching *.js wildcard" > /workspace/test_special_patterns/wildcard.txt && \
        echo "testing x > y redirection" > /workspace/test_special_patterns/redirect.txt && \
        echo "testing a | b pipe" > /workspace/test_special_patterns/pipe.txt && \
        echo "line with #comment" > /workspace/test_special_patterns/comment.txt && \
        echo "CSS \\!important rule" > /workspace/test_special_patterns/bang.txt
        """
        obs = _run_cmd_action(runtime, setup_cmd)
        assert obs.exit_code == 0, 'Failed to set up test files'

        special_patterns = [
            r'x && y \|\| z',  # Shell logical operators (escaping pipe)
            r'function\(\) \{ return x; \}',  # Properly escaped braces and parentheses
            r'\$variable',  # Dollar sign
            # r"`backticks`",            # Backticks
            r'\\n newline',  # Escaped characters
            r'\*\.js',  # Wildcards (escaped)
            r'x > y',  # Redirection
            r'a \| b',  # Pipe (escaped)
            r'#comment',  # Hash
            # r"!important",             # Bang
        ]

        for pattern in special_patterns:
            # Generate the grep command using our helper function
            cmd = grep_to_cmdrun(pattern, '/workspace/test_special_patterns')
            assert 'rg -li' in cmd
            assert 'Below are the execution results of the search command:' in cmd

            # Execute the command
            obs = _run_cmd_action(runtime, cmd)

            # Verify the command executed successfully
            assert 'command not found' not in obs.content
            assert 'syntax error' not in obs.content
            assert 'unexpected' not in obs.content

            # Check that the pattern was found in the appropriate file
            if '&&' in pattern:
                assert 'logical.txt' in obs.content
            elif 'function' in pattern:
                assert 'function.txt' in obs.content
            elif '$variable' in pattern:
                assert 'dollar.txt' in obs.content
            # elif "backticks" in pattern:
            #     assert "backticks.txt" in obs.content
            elif '\\n newline' in pattern:
                assert 'newline.txt' in obs.content
            elif '*' in pattern:
                assert 'wildcard.txt' in obs.content
            elif '>' in pattern:
                assert 'redirect.txt' in obs.content
            elif '|' in pattern:
                assert 'pipe.txt' in obs.content
            elif '#comment' in pattern:
                assert 'comment.txt' in obs.content
            # elif "!important" in pattern:
            #     assert "bang.txt" in obs.content
    finally:
        _close_test_runtime(runtime)
Test patterns with special shell characters.
tests/runtime/test_glob_and_grep.py