Search code examples
environment-variableszshzshrc

Unix shell delay filename expansion


I have a (java) software project with source file is several directories.

If I do the following in my .zshrc file:

J=dir1/*.java dir2/*.java dir3/*.java ...
export J

Then I get errors that the environment is too large

Normally I use a makefile tree that compiles one directory (package) at a time but sometimes the relationship changes are too messy so I do:

javac $J

To recompile the whole lot.

Question: Can I do something to quote the list, like

J="dir1/*.java dir2/*.java dir3/*.java ..."
export J

So that the environment is small but have the variable expands the filename list when it is used.

I know I could something like

javac `eval ls $J`

but I was hoping the invocation could still be simple.


Solution

  • You probably want

    J=(dir1/*.java dir2/*.java dir3/*.java ...)
    

    (no export needed, since .zshrc is sourced). With J set, you can simply run

    javac $J
    

    or more explicitly (without or without the braces)

    javac ${J[@]}
    

    Whether you need quotes depends on which shell options you have enabled, but they aren't necessary by default in zsh.