[英]ZSH/Shell variable assignment/usage
I use ZSH for my terminal shell, and whilst I've written several functions to automate specific tasks, I've never really attempted anything that requires the functionality I'm after at the moment.我将 ZSH 用于我的终端 shell,虽然我编写了几个函数来自动执行特定任务,但我从未真正尝试过任何需要我目前所追求的功能的东西。
I've recently re-written a blog using Jekyll and I want to automate the production of blog posts and finally the uploading of the newly produced files to my server using something like scp.我最近使用 Jekyll 重写了一个博客,我想自动生成博客文章,最后使用 scp 之类的东西将新生成的文件上传到我的服务器。
I'm slightly confused about the variable bindings/usage in ZSH;我对 ZSH 中的变量绑定/使用有点困惑; for example:
例如:
DATE= date +'20%y-%m-%d'
echo $DATE
correctly outputs 2011-08-23 as I'd expect.正如我所料,正确输出 2011-08-23 。
But when I try:但是当我尝试时:
DATE= date +'20%y-%m-%d'
FILE= "~/path/to/_posts/$DATE-$1.markdown"
echo $FILE
It outputs:它输出:
2011-08-23
blog.sh: line 4: ~/path/to/_posts/-.markdown: No such file or directory
And when run with what I'd be wanting the blog title to be (ignoring the fact the string needs to be manipulated to make it more url friendly and that the route path/to doesn't exist)当以我想要的博客标题运行时(忽略需要对字符串进行操作以使其对 url 更友好以及路由路径/到不存在的事实)
ie blog "blog title", outputs:即博客“博客标题”,输出:
2011-08-23
blog.sh: line 4: ~/path/to/_posts/-blog title.markdown: No such file or directory
Why is $DATE printing above the call to print $FILE rather than the string being included in $FILE?为什么在打印 $FILE 的调用上方打印 $DATE 而不是 $FILE 中包含的字符串?
Two things are going wrong here.这里有两件事出了问题。
Firstly, your first snippet is not doing what I think you think it is.首先,你的第一个片段没有做我认为你认为的那样。 Try removing the second line, the
echo
.尝试删除第二行
echo
。 It still prints the date, right?它仍然打印日期,对吗? Because this:
因为这:
DATE= date +'20%y-%m-%d'
Is not a variable assignment - it's an invocation of date
with an auxiliary environment variable (the general syntax is VAR_NAME=VAR_VALUE COMMAND
).不是变量赋值 - 它是使用辅助环境变量调用
date
(一般语法是VAR_NAME=VAR_VALUE COMMAND
)。 You mean this:你是这个意思:
DATE=$(date +'20%y-%m-%d')
Your second snippet will still fail, but differently.您的第二个片段仍然会失败,但会有所不同。 Again, you're using the invoke-with-environment syntax instead of assignment.
同样,您使用的是invoke-with-environment语法而不是赋值。 You mean:
你的意思是:
# note the lack of a space after the equals sign
FILE="~/path/to/_posts/$DATE-$1.markdown"
I think that should do the trick.我认为这应该可以解决问题。
While I know bash very well, I only started using zsh recently;虽然我非常了解 bash,但我最近才开始使用zsh ; there may be zshisms at work here that I'm not aware of.
这里可能有我不知道的zshisms 。
Learn about what a shell calls 'expansion'.了解 shell 所称的“扩展”。 There are several kinds, performed in a particular order:
有几种,按特定顺序执行:
The order of word expansion is as follows:扩词顺序如下:
set -f
is in effectset -f
生效Note that tilde expansion is only performed when the tilde is not quoted;请注意,波浪号扩展仅在波浪号不被引用时执行; viz.:
即:
$ FILE="~/.zshrc"
$ echo $FILE
~/.zshrc
$ FILE=~./zshrc
$ echo $FILE
/home/user42/.zshrc
And there must be no spaces around the =
in variable assignments.变量赋值中的
=
周围不能有空格。
Since you asked in a comment where to learn shell programming, there are several options:由于您在评论中询问在哪里学习 shell 编程,所以有几个选择:
man zsh
man zsh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.