I've written myself a linux program "program" that does something with a regular expression. I want to call the program in the bash shell and pass that regular expression as a command line argument to the program (there are also other command line arguments). A typical regular expression looks like "[abc]_[x|y]". Unfortunately the characters [, ], and | are special characters in bash. Thus, calling "program [abc]_[x|y] anotheragument" doesn't work. Is there a way to pass the expression by using some sort of escape characters or quotation marks etc.? (Calling program "[abc]_[x|y] anotheragument" isn't working either, because it interprets the two arguments as one.)
|
You can either
|
|||
|
|
|
Use single quotes. Single quotes ensure that none of the characters are interpreted.
There are two solutions if you need to embed a single quote:
|
|||
|
|
|
You can use a backslash ( john@awesome:~ # echo \& & |
|||
|
|
|
Although it might not be useful as a regex, some character sequences may be interpreted as Bash variable names. To prevent this from occurring and avoid having them expanded, use single quotes instead of double quotes:
Quote each argument separately (if they need quoting) so they are interpreted as independent arguments. You can also use arrays in some cases:
|
|||
|
|
|
Per
|
|||
|
|
|
Where does the pattern come from? Is it fixed or from a user? Is it the user who is invoking the script on the local system, or someone remote? You use quotes to wrap data to keep the shell from interpreting it. There are two options:
Because Note that |
|||||
|