简体   繁体   English

我如何在不使用变量的情况下在Bash中串联两个字符串?

[英]How do I concatenation two strings in Bash without using variables?

I'm trying to tar a large directory, and I'd like the output file to contain the date. 我正在尝试压缩一个大目录,并且我希望输出文件包含日期。 Here's what I have so far. 到目前为止,这就是我所拥有的。

tar -zcvf "mywebsite website backup" $(date "+%Y-%m-%d %T") public_html

This doesn't quite work though. 不过,这不太有效。 As an example, I'm trying to get the single string below to be the file name. 例如,我试图将下面的单个字符串作为文件名。 The call to date is working fine and I don't need help with that, but I'm not sure how to concatenate the "my website backup" with the output from date without first storing them in variables (which seems ham fisted). 到目前为止的调用工作正常,我不需要这方面的帮助,但是我不确定如何在不先将它们存储在变量中的情况下将“我的网站备份”与日期的输出连接起来(这似乎很困难)。

mywebsite website backup 2001-12-01 12:04:03

Variables and such are interpreted within double quotes (but not within single quotes): 变量等在双引号中解释(但不在单引号中):

tar -zcvf "mywebsite website backup $(date '+%Y-%m-%d %T').tgz" public_html

(I added the .tgz suffix, you apparently forgot.) (我添加了.tgz后缀,您显然忘记了。)

If you had no white space in your file name, you could do it without quotes: 如果文件名中没有空格,则可以不带引号:

tar -zcvf foo_backup_$(date '+%Y-%m-%d_%T').tgz foo/

Or you could turn it around and use the fact that date already accepts a format string: 或者,您可以改掉date并使用date已经接受格式字符串的事实:

tar -zcvf $(date +'foo_backup_%Y-%m-%d_%T.tgz') foo/

(Still needs double quotes around $() if the file name contains white space.) (如果文件名包含空格,则仍然需要在$()周围加上双引号。)

NB: For various reasons it is usually not a good idea to include colons in file names. 注意:由于各种原因,在文件名中包含冒号通常不是一个好主意。

您还可以尝试语法:

tar -zcvf "mywebsite website backup `date \"+%Y-%m-%d %T\"`.tgz" public_html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM