Which quotes does a programmer need?

主宰稳场 提交于 2019-12-22 17:23:09

问题


My keyboard only has normal quotes, not the smart ones.

I have obversed that I need normal ones in cgi development and the backward ones in AWK/SED.

Is there any rule when I should use smart quotes, normal ones and backward ones?

Obviously, I need to edit my keyboard layout to get the smart quotes.


回答1:


If you mean ` by smart quotes, then that is actually called "backquote". Smart quotes are when you type ' and ", but get ‘ and ’ or “ and ” automatically depending on the context. I'm not sure how you would use smart quotes in awk or sed.

In the shell, backquotes, such as `command`, are used to evaluate a command and substitute the result of the command within them into the shell expression being evaluated; it can be used to compute and argument to another command, or to set a variable. For less ambiguity, you can instead use $(command), which makes a lot of quoting rules easier to work out.

In the shell, ' and " are also different. " is used for strings in which you want variable substitution and escape sequences. ' represents a string containing just the characters within the quotes, with not variable interpolation or escape sequences.

So, for example:

$ name=world
$ echo "Hello, $name"
Hello, world
$ echo 'Hello, $name'
Hello, $name
$ echo "Testing \\ escapes"
Testing \ escapes
$ echo 'Testing \\ escapes'
Testing \\ escapes
$ echo `ls`
example-file another-example
$ echo 'ls'
ls
$ echo "ls"
ls

Other scripting languages, such as Perl and Ruby, have similar rules, though there may be slight differences.




回答2:


Smart quotes are for beautiful typesetting. They have nothing to do with programming.

Edit: the quotes you do need.

  • Double quotes: " " they are used for literal strings in many languages
  • Single quotes: ' ' used for literal characters in some languages like C and for strings in languages like javascript and php. (For example if you need to print a string "foo", you could use '"foo"')
  • Back quotes: in UNIX shells, to indicate substitution of the standard output from one command into a line of text defining another command. For example echo ``date\ might execute echo Sat Mar 1 09:43:00 GMT 2008 and print Sat Mar 1 09:43:00 GMT 2008.



回答3:


Backquotes are used a lot in shell/awk/perl programming, and when doing documents in TeX. Other than that, you probably won't use them much.




回答4:


As far as I know, no language requires (or necessarily even supports) "smart quotes" unless you are calling the backtick character ` a smart quote. if that's the case, many language support the backtick. For example, both bash and ruby use the backtick for command substitution.

To answer the question Is there any rule when I should use smart quotes and normal ones?, yes, there is a rule (again, assuming you mean the backtick when you say "smart quotes"). In most languages, different types of quoting give you different types of behavior. The rule is, learn what the behavior is for that particular language then pick the quote that gives you that behavior.




回答5:


Smart quotes are the devil.




回答6:


Smart quotes is word processor feature. When you type "quote" it gets automatically replaced with “quote” or „quote”. I think you got your nomenclature wrong.




回答7:


Just an FYI, another term for 'smart quotes' (which I have never heard of that before), is grave accent.

I think the rules have been laid out pretty clearly in previous answers.




回答8:


$ /usr/games/fortune



来源:https://stackoverflow.com/questions/679108/which-quotes-does-a-programmer-need

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!