博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell script中引号的用法
阅读量:6311 次
发布时间:2019-06-22

本文共 3625 字,大约阅读时间需要 12 分钟。

Quoting a single character with the backslash

You can prevent the shell from interpreting a character by placing a backslash ("\") in front of it. Here is a shell script that can delete any files that contain an asterisk:

echo This script removes all files that echo contain an asterisk in the name. echo echo Are you sure you want to remove these files\? rm -i *\**

因为在shell中,?与*都是特殊字符,会被shell当做特殊命令来对待,其实写shell script时候特定的命令,就像在shell中执行一样。

所以这个功能同样也适用于shell自己,使用backslash("\")。

The backslash was also necessary before the question mark, which is also a shell meta-character. Without it, the shell would look for all files that match the pattern "files?." If you had the files "files1" and "files2" the script would print out

Are you sure you want to remove these files1 files2

which is not what you want.

The backslash is the "strongest" method of quotation. It works when every other method fails. If you want to place text on two or more lines for readability, but the program expects one line, you need a line continuation character. Just use the backslash as the last character on the line:

% echo This could be \ a very \ long line\! This could be a very long line! %

This escapes or quotes the end of line character, so it no longer has a special meaning. In the above example, I also put a backslash before the exclamation point. This is necessary if you are using the C shell, which treats the "!" as a special character. If you are using some other shell, it might not be necessary.

 

Strong Quoting with the Single Quotes

When you need to quote several character at once, you could use several backslashes:

% echo a\ \ \ \ \ \ \ b

(There are 7 spaces between 'a' and 'b'.) This is ugly but works. It is easier to use pairs of quotation marks to indicate the start and end of the characters to be quoted:

% echo 'a b'

(The HTML ruins the formatting. Imagine that there are 7 spaces between the a and b. -Bruce) Inside the single quotes, you can include almost all meta-characters:

% echo 'What the *heck* is a $ doing here???' What the *heck* is a $ doing here???

The above example uses asterisks, dollar signs, and question marks meta-characters. The single quotes should be used when you want the text left alone. If you are using the C shell, the "!" character may need a backslash before it. It depends on the characters next to it. If it is surrounded by spaces, you don't need to use a backslash.

Weak Quotes with the Double Quotes

Sometimes you want a weaker type of quoting: one that doesn't expand meta-characters like "*" or "?," but does expand variables and does command substitution. This can be done with the double quote characters:

% echo "Is your home directory $HOME?" Is your home directory /home/kreskin/u0/barnett? % echo "Your current directory is `pwd`" Your current directory is /home/kreskin/u0/barnett

Once you learn the difference between single quotes and double quotes, you will have mastered a very useful skill. It's not hard. The single quotes are stronger than the double quotes. Got it? Okay. And the backslash is the strongest of all.

Quotes within Quotes

While having two types of quotes (three if you count the backslash) might seem confusing, in reality it provides you with several ways to solve the same problems. You can put either quotes inside the other. If you want to quote single quotes, use double quotes around it. To quote double quotes, use single quotes. Heck, it's easier to show you:

% echo "Don't do that" Don't do that % echo 'The quote of the day is: "TGIF"' The quote of the day is: "TGIF" %

转载于:https://www.cnblogs.com/jack204/archive/2011/11/01/2231408.html

你可能感兴趣的文章
java 基于QRCode、zxing 的二维码生成与解析
查看>>
关于职业规划的一些思考
查看>>
img垂直水平居中与div
查看>>
Fabrik – 在浏览器中协作构建,可视化,设计神经网络
查看>>
防恶意注册的思考
查看>>
http2-head compression
查看>>
C# 命名空间
查看>>
订餐系统之同步美团商家订单
查看>>
使用ArrayList时设置初始容量的重要性
查看>>
Java Web-----JSP与Servlet(一)
查看>>
Maven搭建SpringMVC+Mybatis项目详解
查看>>
关于量子理论:最初无意的简化,和一些人有意的强化和放大
查看>>
CentOS 6.9通过RPM安装EPEL源(http://dl.fedoraproject.org)
查看>>
“区块链”并没有什么特别之处
查看>>
没有功能需求设计文档?对不起,拒绝开发!
查看>>
4星|《先发影响力》:影响与反影响相关的有趣的心理学研究综述
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
python之 列表常用方法
查看>>
vue-cli脚手架的搭建
查看>>
在网页中加入百度搜索框实例代码
查看>>