Search code examples
variableszsh

How to create user defined variables in ZSH?


I need to write a script for running a program with command in terminal with some variables (user defined). Such as;

./program VAR1 VAR2 VAR3.

However, my script must be in ZSH since it contains some loops that works well with ZSH. This is the code:

    #!/usr/bin/env zsh

    set VAR1=$1
    set VAR2=$2
    set VAR3=$3
  
    for ((i = 0; i < 41; i++)); do
    
    ./program.csh $VAR1 $VAR2 $VAR3

    done

    echo program runs!

I know that this would work in CSH but I don't know how to convert this to ZSH. Thanks for the help in advance!


Solution

  • The only real difference is that set is used to set shell options or positional parameters, not ordinary variables.

    #!/usr/bin/env zsh
    
    VAR1=$1
    VAR2=$2
    VAR3=$3
    
    for ((i = 0; i < 41; i++)); do
    
    ./program.csh $VAR1 $VAR2 $VAR3
    
    done
    
    echo program runs!
    

    You are really going to want to become more familiar with zsh if you plan on writing scripts in it; this is an extremely basic feature of all POSIX-style shells.