LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2017, 09:06 AM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
Single quote marks to protect symbols from interpretation as shell script punctuation


Example from man find:
Code:
find . -type f -exec file '{}' \;

Runs `file' on every file in or 
below the current directory.  
Notice that the braces are  enclosed  
in  single quote  marks to protect them 
from interpretation as shell script punctuation.  
The semicolon is similarly protected by the 
use of a backslash, though single quotes could 
have been used in that case also.
In same man page, there is an example shown below
that do not have single quotes protection for {}.
Code:
find repo/ -exec test -d {}/.svn \; -or \
       -exec test -d {}/.git \; -or -exec test -d {}/CVS \; \
       -print -prune
Why are above braces pair, {}, not protected with single quotes?
This seems inconsistent and thus confusing.
When is protection from bash interpretation needed and when is it not needed?

Thank you.
 
Old 02-25-2017, 11:40 AM   #2
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
I suspect that in the latter example, it wants to be able to process requests as if they are a glob: https://en.wikipedia.org/wiki/Glob_(programming)

Not certain on that tho.
 
Old 02-25-2017, 12:06 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
The "{" and "}" characters are special to the shell only in certain constructs, and a simple "{}" even unquoted is not one of those constructs. As an example, "{" can't begin a block of shell commands unless it is followed by a whitespace character, and "}" can't end the block unless there is a ";" or newline following the last command in the block.
 
Old 02-25-2017, 02:07 PM   #4
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
The "{" and "}" characters are special to
the shell only in certain constructs,
and a simple "{}" even unquoted is
not one of those constructs
.

As an example, "{" can't begin a block of shell commands
unless it is followed by a whitespace character,
and "}" can't end the block unless there is a ";"
or newline following the last command in the block.
Thank you for trying to help.
But I am not able to follow your explanation.

For the underlined fragment above, can you link to some
rules on how to determine when a construct needs to be escaped?

Code:
As an example, "{" can't begin a block of 
shell commands unless it is followed by 
a whitespace character, and "}" can't end 
the block unless there is a ";" or newline 
following the last command in the block
Is the boldfaced sentence above the only rule?
As for "a block of shell commands" do you mean commands
to be processed by the shell or by the command
preceding the "block of shell commands"?

In your example, are you saying for such construct,
it needs to be escaped like so?
Code:
command1 option1A "{ command2 option2A option2B argument2A; }"
As I understand it, when we want to prevent
the shell from interpreting characters that
we want to pass on to its preceding command
(e.g. command1), we escape those characters
with a backslash, enclose it with double quotes,
or single quotes.

I tried testing the first example without
escaping {}, and it still works.
Code:
~ $ find . -maxdepth 1 -type f -exec file {} \;
and I did not escape ";", and it produces and error.
Code:
~ $ find . -maxdepth 1 -type f -exec file {} ;
find: missing argument to `-exec'
So ";" is special to the shell and it was consumed by the shell
and was never passed to -exec.

A list of rules and types of character that MUST be escaped
(i.e. special to the shell) would clear things up.

Now I am even more confused!
 
Old 02-25-2017, 05:56 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191Reputation: 2191
Just about all of the characters that are sometimes, or even usually, special to the shell have cases where they are not. Within a double-quoted string, for example, the backslash character retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Outside of a single-quoted string, the "$" character is mostly special, except when it stands alone or is followed by characters like % or ^ that could not be part of an expansion. There are far too many cases to memorize, but after a while you get to know common ones, like {}, that do not need to be quoted (though quoting them is harmless, too).

Quote:
Originally Posted by fanoflq View Post
In your example, are you saying for such construct,
it needs to be escaped like so?
Code:
command1 option1A "{ command2 option2A option2B argument2A; }"
Only if you wanted that string to be taken literally as a second argument to command1. But, note that
Code:
command1 a {b c d} e
is a command with 5 arguments, "a", "{b", "c", "d}", and "e". Neither of those curly braces are in a situation where they are special.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is US style of punctuation with quotation marks this way? Mr. Alex General 26 01-06-2013 08:47 PM
[SOLVED] How to pass a single quote inside of a single quote... trist007 Linux - Newbie 2 02-09-2011 07:07 PM
[SOLVED] escaping a single quote from a bash script atbrew Programming 8 07-21-2010 09:21 AM
missing em dash & other punctuation marks in font warnerwhite Linux - Newbie 0 01-28-2009 01:01 PM
how to use single quote in bash shell like: echo ''\''' linuxtyh Linux - General 6 12-11-2008 11:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration