ZSH/Shell variable assignment/usage ZSH/Shell variable assignment/usage unix unix

ZSH/Shell variable assignment/usage


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:

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). 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 signFILE="~/path/to/_posts/$DATE-$1.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.


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

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 -f is in effect
  6. quote removal, always performed last

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:

  • 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 will find 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