Assigning / Using ZSH / Shell Variables

I use ZSH for my terminal shell, and although I wrote several functions to automate specific tasks, I never tried anything that requires the functionality that I find at the moment.

I recently rewrote a blog using Jekyll, and I want to automate the blog posts and finally upload the newly created files to my server using something like scp.

I am a bit confused about variable bindings / usage in ZSH; eg:

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

correctly displays 2011-08-23, as I expected.

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 starting up with what I would like to get in the blog title (ignoring the fact that you need to manipulate the line to make it more URL friendly and that the path / path of the route does not 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 

Why print $ DATE over a print call to $ FILE and not the line included in $ FILE?

+6
source share
2 answers

Two things are wrong here.

First, your first fragment does not do what I think you think it is. Try deleting the second line, echo . He's still printing the date, isn't he? Because it:

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

It is not a variable assignment - it is a date call with an auxiliary environment variable (the general syntax is VAR_NAME=VAR_VALUE COMMAND ). You mean this:

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

The second snippet will still fail, but in a different way. Again, instead of assigning, you use the invoke-with-environment syntax. Do you mean:

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

I think this was supposed to do the trick.

Disclaimer: although I know bash well, I just recently started using zsh; there may be zshisms that I do not know.

+16
source

Find out what the shell is causing the "extension". There are several types made in a certain order:

The word order is as follows:

  • tilde expansion
  • parameter extension
  • team substitution
  • arithmetic expansion
  • pathname extension , unless set -f is working
  • delete quotation , always done last

Note that tilde expansion is performed only when the tilde is not quoted; namely:.

 $ FILE="~/.zshrc" $ echo $FILE ~/.zshrc $ FILE=~./zshrc $ echo $FILE /home/user42/.zshrc 

And there should be no spaces around = in variable assignments.

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

  • Read the man zsh shell man zsh page
  • Read the POSIX shell spec http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html , especially if you want to run your scripts on different operating systems (and you will be in this situation one day!)
  • Read books on shell programming.
  • Go to the usenet comp.unix.shell newsgroup, where many shell wizards answer questions.
+7
source

Source: https://habr.com/ru/post/895711/


All Articles