# Referencing Launch Arguments in Scripts ## Description Scripts ran through FileWave have the option to supply 'Launch Arguments'. These are referenced from the script but are not included in the body of the script. They may be supplied to any of the following: - [Fileset Scripts](https://kb.filewave.com/books/filesets-payloads "Filesets / Payloads") - [Custom Fields](https://kb.filewave.com/books/custom-fields/page/custom-fields "Custom Fields") - Policy Blocker Scripts Often Admins feel that there is a limit of 9 'Launch Arguments' through FileWave, but the below will demonstrate this is not the case. ## Information The Launch Arguments, known as [Positional Parameters](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Positional-Parameters), are referenced as follows:
macOS/LinuxWindows PowershellWindows Bat
First Argument$1`$args[0]`%1
Second Argument$2`$args[1]`%2
Third Argument$3`$args[2]`%3
More may be added and each is referenced in turn by its positional place as in the above table. ## Considerations Certain shell types behave differently. This may particularly show when referencing the 10th or higher supplied argument.

Although two solutions have been supplied, zsh is recommended to stay in line with Apple's policy: [https://support.apple.com/en-gb/HT208050](https://support.apple.com/en-gb/HT208050)

### bash and sh The following example was hoped to print the first 3 positional parameters, followed by the 10th and 11th. **bash\_test.sh** ```shell #!/bin/bash echo $1 echo $2 echo $3 echo $10 echo $11 exit 0 ``` However, if the character '1' was supplied as single argument the following would be observed when ran: **bash\_test.sh** ```shell ./bash_test.sh 1 1 10 11 ``` The bash and sh shells are examples which treat $10 as ${1}0, $11 as ${1}1, etc.; returning the value of $1 and then appending the additional character (0 or 1 in this example). Additional reference: - [https://www.oreilly.com/library/view/bash-cookbook/0596526784/ch05s07.html](https://www.oreilly.com/library/view/bash-cookbook/0596526784/ch05s07.html) - [https://www.oreilly.com/library/view/bash-cookbook/0596526784/ch05s04.html](https://www.oreilly.com/library/view/bash-cookbook/0596526784/ch05s04.html) As such the above output is equivalent to:
DescriptionOutput
$1Returns first argument1
$2Returns second argument2
$3Returns nothing, no third argument
$10Returns first argument, followed by the '0' character10
$11Returns first argument, followed by the '1' character11
To ensure the correct positional parameters are referenced, the variables must be explicitly set to achieve the correct output. The following shows the corrected script. **bash\_test.sh** ```shell #!/bin/bash echo $1 echo $2 echo $3 echo ${10} echo ${11} exit 0 ``` Now when executed with 11 positional parameters, the expected output is displayed: **bash\_test.sh** ```shell ./bash_test.sh var1a var2b var3c var4d var5e var6f var7g var8h var9i var10j var11k var1a var2b var3c var10j var11k ``` ### zsh Not all shells act the same. An alternate to the above would be zsh. Taking the above example as a zsh script: **zsh\_test.sh** ```shell #!/bin/zsh echo $1 echo $2 echo $3 echo $10 echo ${11} exit 0 ``` Given the same 11 arguments, this would output: **zsh\_test.sh** ```shell ./zsh_test.sh var1a var2b var3c var4d var5e var6f var7g var8h var9i var10j var11k var1a var2b var3c var10j var11k ``` Unlike bash and sh, zsh considers $10 to be the 10th argument, $11 the 11th argument and so on and so forth. Notice zsh may use either format. ## Conclusion Often admins are confused with output results when using 10 or more arguments. Armed with the above knowledge, this should assist structuring scripts. ## Related Content - [Custom Fields](https://kb.filewave.com/books/custom-fields/page/custom-fields "Custom Fields") - [Filesets / Payloads](https://kb.filewave.com/books/filesets-payloads "Filesets / Payloads")