The text 'hello "value"
as seen by the shell starts with a single quote and never ends it. You'll get a continuation prompt ($PS2
, often >
) asking for the rest of the string.
To search for something like this you would need to escape the quotes (both sorts) from the shell, which you can do like this
"'hello \"value\""
Or if you don't want to escape all the literal double quotes, quote the initial single quote and leave the rest of the string single-quoted
"'"'hello "value"'
^1 ^3 ^4
^2
- 1-2 double-quoted string containing the initial leading single-quote mark
- 3-4 single-quoted string containing literal double-quote marks
If you have particularly complicated strings there would be nothing wrong with you reading the string from the script, where the shell can be instructed not to try to parse it, rather than on the command line, where it will attempt to parse it
#!/bin/bash
[[ $# -eq 0 ]] && IFS= read -rp "Enter search string: " item
rg -F "${1:-$item}"
Make the script executable and put it somewhere in your $PATH
. If you provide something on the command line it will use it. If you don't, it will prompt for the string and you can enter one with as much complexity as you like.
As a footnote, possibly part of the confusion is the expectation from the Windows/DOS world that the program itself parses the command line. This is not the case in the UNIX/Linux world, where the shell parses the command line and passes the resulting arguments to the command. There is no way to tell to the shell not to parse the command line - that's what it does - so you need to work with that or else bypass the shell. Related reading is How to escape quotes in shell? and What is the difference between the “…”, '…', $'…', and $“…” quotes in the shell?
, which explain in much more detail about quoting quotes, etc.
brute-force way
is to backslash the shell metacharacters on the command line,as in:\'hello\ \"value\"
Admittedly, this isn't the best of ways, but gets the job done.