![]() This isn’t necessary, but if you do this you won’t have to remember how quotes and escapes work on different platforms. Id like to escape single and double quotes while running a command under a different user. Use character codes for literal quote characters. Im having trouble with escaping characters in bash. ![]() On Windows (cmd and PowerShell) surround code with double quotes. escaping single-quotes Yes, I know that I can write. On Linux (bash) surround code with single quotes. In the first example, we used the dollar () symbol, while the second example shows the usage of multiple single quotes. For Python one-liners, you can swap single quotes and double quotes. Perl has a handy feature for avoiding confusion over quotes: you can use q inside code rather than single or double quotes. 6 Answers Sorted by: 10 Dennis points out the usual alternatives in his answer (single-in-double, double-in-single, single quoted strings concatenated with escaped single quotes you already mentioned Perl's customizable quote operators), but maybe this is the direct answer you were looking for. There are ways to fix the problems with cmd and PowerShell without breaking bash. This is particularly puzzling since PowerShell follows basically the same quoting rules as bash: double quotes interpolate but single quotes do not. The corresponding Python example would be python -c 'print("hello\n")'įor reasons I don’t understand, the Perl example above works from cmd, but the Python example does not.Īnd neither example works in PowerShell. The single quotes tell the shell to pass what’s inside to perl without doing anything to it. So if you want to send a string to a program unmolested, you surround it with single quotes.įor example, suppose I want to write a Perl one-line program to print hello. Bashīash interprets text inside double quotes but not in single quotes. Enclosing characters in single quotes preserves the literal value of each character within the quotes. We want to be able to use bash on Linux, and cmd or PowerShell on Windows, ideally with the same code in all three shells. You should stick to single quotes, unless you want to interpolate variables. IMHO, the real answer is that you can't escape single-quotes within single-quoted strings. The goal this post is to show how to call Perl and Python one-liners from a shell.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |