Skip to main content

Referencing Launch Arguments in Scripts

Description

Scripts ranrun throughby FileWave havecan receive Launch Arguments. The arguments are supplied by FileWave and referenced by the optionscript, tobut supply 'Launch Arguments'.  Thesethey are referencednot fromwritten into the script but are not included in the body of the script.body.

They may be supplied to any of the following:

Often Admins feelsometimes thatassume thereFileWave is alimited limit ofto 9 'Launchlaunch Arguments'arguments. throughIt FileWave,is butnot; the belowshell willyou demonstrateuse thisdetermines ishow notyou thereference case.arguments 10 and higher.

Information

The Launch Arguments,Arguments knownare aspositional Positionalparameters. ParametersThey are referenced asdifferently follows:depending on the script type:

  macOS/Linux Windows Powershell Windows Bat
First Argument $1 $args[0] %1
Second Argument $2 $args[1] %2
Third Argument $3 $args[2] %3

MoreYou maycan beadd addedmore arguments and reference each is referenced in turnone by its positional place as in the above table.position.

Considerations

CertainShell shellbehavior typesmatters behave differently.  This may particularly showmost when referencingyou reference the 10th argument or higher supplied argument.higher.

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

bash and sh

The following bash example wasis hopedintended to print the first 3 positional parameters, followed by the 10th and 11th.

bash_test.sh

#!/bin/bash

echo $1
echo $2
echo $3
echo $10
echo $11

exit 0

However,If ifyou thepass characteronly '1'one wasargument, supplied1, asbash singletreats argument$10 theand following$11 woulddifferently bethan observedmany whenadmins ran:expect:

bash_test.sh

./bash_test.sh 1
1


10
11

The bash and sh shells are examples which treat $10 as ${1}0,0, $11 as ${1}1,1, etc.;and returningso on. In this example, they return the value of $1 and then appendingappend the additionalextra character (0 or 1 in this example).  character.

Additional reference: references:

As such the aboveThat output is equivalentmaps to:

  Description Output
$1 Returns first argument 1
$2 Returns second argument 2
$3 Returns nothing, no third argument  
$10 Returns first argument, followed by the '0' character 10
$11 Returns first argument, followed by the '1' character 11

ToFor ensurebash theand correctsh, wrap two-digit positional parameters arein referenced,braces so the variablesscript must be explicitly set to achievereferences the correctintended output.argument.

The followingcorrected showsscript uses braces for the corrected10th script.and 11th arguments.

bash_test.sh

#!/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

./bash_test.sh var1a var2b var3c var4d var5e var6f var7g var8h var9i var10j var11k
var1a
var2b
var3c
var10j
var11k

zsh

Notzsh allhandles shellsthis actdifferently. With the same.  An alternate to the above would be zsh.  Taking the abovesame example asin a zsh script:zsh:

zsh_test.sh

#!/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

./zsh_test.sh var1a var2b var3c var4d var5e var6f var7g var8h var9i var10j var11k
var1a
var2b
var3c
var10j
var11k

Unlike bash and sh, zsh considerstreats $10 to beas the 10th argument,argument and $11 as the 11th argument and so on and so forth.  Noticeargument. zsh maycan use either format.

Conclusion

OftenIf adminsa arescript confused with output results when usinguses 10 or more arguments.launch Armedarguments, withcheck the aboveshell knowledge,before thistroubleshooting shouldthe assistFileWave structuringside. scripts.In bash or sh, use ${10}, ${11}, and the same brace pattern for later arguments. In zsh, $10 and ${10} both refer to the 10th argument.