ZSH/Shell变量分配/用法 [英] ZSH/Shell variable assignment/usage

查看:57
本文介绍了ZSH/Shell变量分配/用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在终端外壳中使用ZSH,尽管我编写了一些函数来自动执行特定任务,但我从未真正尝试过需要此刻所需功能的任何事情.

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.

我最近使用Jekyll重新编写了一个博客,我想自动化博客文章的生成,最后要使用诸如scp之类的东西将新生成的文件上传到我的服务器上.

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.

我对ZSH中的变量绑定/用法有些困惑;例如:

I'm slightly confused about the variable bindings/usage in ZSH; for example:

DATE= date +'20%y-%m-%d'
echo $DATE

正确地输出了2011-08-23.

correctly outputs 2011-08-23 as I'd expect.

但是当我尝试:

DATE= date +'20%y-%m-%d'
FILE= "~/path/to/_posts/$DATE-$1.markdown"
echo $FILE

它输出:

2011-08-23
blog.sh: line 4: ~/path/to/_posts/-.markdown: No such file or directory

然后运行我想要的博客标题(忽略该事实,即需要对该字符串进行操作以使其对URL更友好,并且路由路径/目的地不存在)

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)

即博客博客标题",输出:

i.e. blog "blog title", outputs:

2011-08-23
blog.sh: line 4: ~/path/to/_posts/-blog title.markdown: No such file or directory

为什么$ DATE打印在打印$ FILE的调用上方而不是$ FILE中包含的字符串?

Why is $DATE printing above the call to print $FILE rather than the string being included in $FILE?

推荐答案

这里出现了两件事.

首先,您的第一个代码片段没有执行我认为您认为的操作.尝试删除第二行,即 echo .它仍然会打印日期,对不对?因为这个:

Firstly, your first snippet is not doing what i think you think it is. Try removing the second line, the echo. It still prints the date, right? Because this:

DATE= date +'20%y-%m-%d'

不是变量分配-它是对 date 的调用,带有辅助环境变量(一般语法为 VAR_NAME = VAR_VALUE COMMAND ).您的意思是:

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). 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. 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.

免责声明:虽然我非常了解bash,但我最近才开始使用zsh;这里可能有我不知道的zshism.

Disclaimer: while i know bash very well, i only started using zsh recently; there may be zshisms at work here that i'm not aware of.

这篇关于ZSH/Shell变量分配/用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆