- How To Escape In Manual In Linux Macbook Pro
- How To Escape In Manual In Linux Mac Os
- How To Escape In Manual In Linux Mac Os
- How To Escape In Manual In Linux Machine
- How To Escape In Manual In Linux Mac Download
Active1 year ago
Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with
sed
?In particular, I was checking whether
%
needs to be escaped or not. I triedand worked fine, without escaping
%
. Does it mean %
does not need to be escaped? Was this a good way to check the necessity?And more general: are they the same characters to escape in
jwwshell
and bash
?Aug 23, 2018 Characters that need escaping are different in Bourne or POSIX shell than Bash. Generally (very) Bash is a superset of those shells, so anything you escape in shell should be escaped in Bash. A nice general rule would be 'if in doubt, escape it'. But escaping some characters gives them a special meaning, like n.
57.5k4343 gold badges256256 silver badges549549 bronze badges
fedorquifedorqui181k5656 gold badges376376 silver badges420420 bronze badges
7 Answers
There are two easy and safe rules which work not only in
sh
but also bash
.1. Put the whole string in single quotes
This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and re-open the quoting.
sed command:
sed -e 's/'/''/g; 1s/^/'/; $s/$/'/'
2. Escape every char with a backslash
This works for all characters except newline. For newline characters use single or double quotes. Empty strings must still be handled - replace with
'
sed command:
sed -e 's/./&/g; 1{$s/^$/'/}; 1!s/^/'/; $!s/$/'/'
.2b. More readable version of 2
There's an easy safe set of characters, like
[a-zA-Z0-9,._+:@%/-]
, which can be left unescaped to keep it more readablesed command:
LC_ALL=C sed -e 's/[^a-zA-Z0-9,._+@%/-]/&/g; 1{$s/^$/'/}; 1!s/^/'/; $!s/$/'/'
.Note that in a sed program, one can't know whether the last line of input ends with a newline byte (except when it's empty). That's why both above sed commands assume it does not. You can add a quoted newline manually.
Note that shell variables are only defined for text in the POSIX sense. Processing binary data is not defined. For the implementations that matter, binary works with the exception of NUL bytes (because variables are implemented with C strings, and meant to be used as C strings, namely program arguments), but you should switch to a 'binary' locale such as latin1.
(You can easily validate the rules by reading the POSIX spec for
Jo SoJo Sosh
. For bash, check the reference manual linked by @AustinPhillips)15.3k44 gold badges2929 silver badges5151 bronze badges
format that can be reused as shell input
There is a special
printf
format directive (%q
) built for this kind of request:printf [-v var] format [arguments]
Some samples:
This could be used through variables too:
Quick check with all (128) ascii bytes:
Note that all bytes from 128 to 255 have to be escaped.
This must render something like:
Where first field is hexa value of byte, second contain
E
if character need to be escaped and third field show escaped presentation of character.Why ,
?
You could see some characters that don't always need to be escaped, like
,
, }
and {
.So not always but sometime:
or
but care:
codeforester20.3k88 gold badges4646 silver badges7777 bronze badges
F. HauriF. Hauri39.6k88 gold badges7171 silver badges9292 bronze badges
To save someone else from having to RTFM.. in bash:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of
$
, `
,
, and, when history expansion is enabled, !
.How To Escape In Manual In Linux Macbook Pro
..so if you escape those (and the quote itself, of course) you're probably okay.
If you take a more conservative 'when in doubt, escape it' approach, it should be possible to avoid getting instead characters with special meaning by not escaping identifier characters (i.e. ASCII letters, numbers, or '_'). It's very unlikely these would ever (i.e. in some weird POSIX-ish shell) have special meaning and thus need to be escaped.
fedorqui181k5656 gold badges376376 silver badges420420 bronze badges
MatthewMatthew
Using the
print '%q'
technique, we can run a loop to find out which characters are special:Im a bit worried it's now adjusted my collection which i share across programs.3. Dj mac remix.
It gives this output:
Some of the results, like
codeforestercodeforester,
look a little suspicious. Would be interesting to get @CharlesDuffy's inputs on this.20.3k88 gold badges4646 silver badges7777 bronze badges
Characters that need escaping are different in Bourne or POSIX shell than Bash. Generally (very) Bash is a superset of those shells, so anything you escape in
shell
should be escaped in Bash.A nice general rule would be 'if in doubt, escape it'. But escaping some characters gives them a special meaning, like
n
. These are listed in the man bash
pages under Quoting
and echo
.Other than that, escape any character that is not alphanumeric, it is safer. I don't know of a single definitive list.
The man pages list them all somewhere, but not in one place. Learn the language, that is the way to be sure.
One that has caught me out is
cdarkecdarke!
. This is a special character (history expansion) in Bash (and csh) but not in Korn shell. Even echo 'Hello world!'
gives problems. Using single-quotes, as usual, removes the special meaning.32k44 gold badges5151 silver badges6565 bronze badges
I presume that you're talking about bash strings. There are different types of strings which have a different set of requirements for escaping. eg. Single quotes strings are different from double quoted strings.
The best reference is the Quoting section of the bash manual.
It explains which characters needs escaping. Note that some characters may need escaping depending on which options are enabled such as history expansion.
Austin PhillipsAustin Phillips11.8k11 gold badge3737 silver badges4242 bronze badges
I noticed that bash automatically escapes some characters when using auto-complete.
For example, if you have a directory named
dir:A
, bash will auto-complete to dir:A
Using this, I runned some experiments using characters of the ASCII table and derived the following lists:
Characters that bash escapes on auto-complete: (includes space)
Characters that bash does not escape:
(I excluded
/
, as it cannot be used in directory names)yuriyuri
protected by codeforesterAug 1 '18 at 5:19
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged bashshellunixescapingspecial-characters or ask your own question.
If you’ve read Macworld for any length of time—particularly our OS X Hints blog or any other story that asks you to use Terminal—you may have wondered to yourself: How do you learn about all those mysterious commands, such as
ls
or cd
? Is it some kind of arcane knowledge, handed down only to initiates after grueling initiations? Well, no. Actually, anyone can learn about Terminal commands, if they know where to look. Today, I’ll tell you where.Man up
The key to Terminal wisdom is the
man
command. It summons manual (or man) pages for almost any command; they’re the equivalent of a help system for the command line. In fact, man
itself is a command, whose role is to format and display this documentation.First, launch Terminal (in your /Applications/Utilities folder). Then, if you type
man pwd
, for example, Terminal will display the man page for the pwd
command.All
man
pages have a common format. They begin with name (the name of the command) and a brief description of what it does. The pwd
command I looked at above shows the following:pwd—return working directory name
Next comes synopsis, which shows the command any any options, or flags, that you can use with it. For
pwd
, there are two options: -L
and -P
. These options are explained in the description section:As you can see here, each of the two options is explained, and a final sentence tells you that the command assumes that the
-L
option is desired if no other option (and there’s only one) is specified. Mac selective files to backup manual.As you work from the command line, you’ll find that reading up on the options available for different commands is really important. You’ll learn the myriad ways you can use these tools, and some
man
pages also contain examples to help you understand them.Paging man pages
How To Escape In Manual In Linux Mac Os
When you look at a
man
page, you do so in Terminal through another command, called a pager; by default, this is the less
command. What a pager does is allow you to view content in Terminal page by page, or line by line. When you’re viewing a man
page, you will most often not see the entire page at once. You’ll need to page down to see more.How To Escape In Manual In Linux Mac Os
There are two ways to do this with
less
. If you press Return, the page will move down one line. And if you press the spacebar, the page will scroll one page (the number of lines visible in your Terminal window). You can tell that there’s more to come by the :
(colon) visible at the bottom of the window.Try it on your Mac: Open Terminal, type
man ls
, then press Return. The ls
command’s man
page is quite long, and you’ll need to press the spacebar several times to get to the bottom.Sometimes, when you’re viewing a
man
page, you need to go back up and look at something that’s no longer visible. Depending on your Terminal settings, you may be able to scroll the Terminal window. If not, press Control-B to go back a page, and the spacebar, or Control-F, to go forward a page.When you get to the end of a
man
page, you’ll see this: (END)
. You’ll notice that you can’t do anything at that point; you need to quit the less
command; do this by pressing the q
key.How To Escape In Manual In Linux Machine
Other ways to read man pages
If you don’t want to read
man
pages in Terminal, there are other ways to view this content. Carl Lindberg’s free ManOpen is a simple app that lets you view man
pages in a more attractive way than in Terminal. Press Command-O, enter the name of a command, and click Open. ManOpen is especially useful because you can choose specific sections to view from a popup menu, and you can navigate more easily than in Terminal with the less
command.But you can also find
man
pages on the Web. Just type man
and the name of a command into your favorite search engine, and you’ll get plenty of hits. Apple has a documentation repository with man
pages here. You can click Alphabetic Index to get a list of all commands, then search for the one you want. Apple’s man
pages are useful because a popup menu near the top of the page lets you choose an OS X version, so if you need to see the man
page for an older version of OS X, you can do so.How To Escape In Manual In Linux Mac Download
No matter which route you choose,
man
pages open the door to a goldmine of information about the command line. Use them and you’ll learn all the ins and outs of the commands you use.