bash ZSH/Shell 变量赋值/使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7168043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:57:19  来源:igfitidea点击:

ZSH/Shell variable assignment/usage

macosbashshellunixzsh

提问by HaaR

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; for example:

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

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-.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 更友好并且路由路径/to 不存在的事实)

i.e. 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?

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

回答by Tom Anderson

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. It still prints the date, right? Because this:

首先,你的第一个片段没有做我认为你认为的那样。尝试删除第二行,echo. 它仍然打印日期,对吗?因为这:

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

Is not a variable assignment - it's an invocation of datewith an auxiliary environment variable (the general syntax is VAR_NAME=VAR_VALUE COMMAND). You mean this:

不是变量赋值 - 它是date对辅助环境变量的调用(一般语法是VAR_NAME=VAR_VALUE COMMAND)。你是这个意思:

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:

你的第二个片段仍然会失败,但不同。同样,您使用的是 invoke-with-environment 语法而不是赋值。你的意思是:

# note the lack of a space after the equals sign
FILE="~/path/to/_posts/$DATE-.markdown"

I think that should do the trick.

我认为这应该可以解决问题。

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.

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

回答by Jens

Learn about what a shell calls 'expansion'. There are several kinds, performed in a particular order:

了解 shell 所说的“扩展”。有几种类型,按特定顺序执行:

The order of word expansion is as follows:

单词扩展顺序如下:

  1. tilde expansion
  2. parameter expansion
  3. command substitution
  4. arithmetic expansion
  5. pathname expansion, unless set -fis in effect
  6. quote removal, always performed last
  1. 波浪号扩展
  2. 参数扩展
  3. 命令替换
  4. 算术展开
  5. 路径名扩展,除非set -f有效
  6. 引用删除,总是最后执行

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.

并且=in 变量赋值周围不能有空格。

Since you asked in a comment where to learn shell programming, there are several options:

由于您在评论中询问在哪里学习 shell 编程,因此有几个选项:

  • Read the shell's manual page man zsh
  • Read the specification of the POSIX shell, http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html, especially if you want to run your scripts on different operating systems (and you willfind yourself in that situation one fine day!)
  • Read books about shell programming.
  • Hang out in the usenet newsgroup comp.unix.shell where a lot of shell wizards answer questions
  • 阅读 shell 的手册页 man zsh
  • 阅读 POSIX shell 的规范,http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html,特别是如果你想在不同的操作系统上运行你的脚本(你发现自己处于这种情况下美好的一天!)
  • 阅读有关 shell 编程的书籍。
  • 在 usenet 新闻组 comp.unix.shell 中闲逛,那里有很多 shell 向导回答问题