How to pass ten or more arguments to a bash script from Pipelines
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Problem
When you execute a bash script in a Bitbucket Pipelines build and pass ten or more arguments to the script, the first nine arguments are passed correctly, but the tenth is not.
Assume the following bash script named myscript.sh
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
echo "The first argument is: $1"
echo "The second argument is: $2"
echo "The third argument is: $3"
echo "The fourth argument is: $4"
echo "The fifth argument is: $5"
echo "The sixth argument is: $6"
echo "The seventh argument is: $7"
echo "The eighth argument is: $8"
echo "The ninth argument is: $9"
echo "The tenth argument is: $10"
Assume the following command that runs this script during a Pipelines build and passes ten arguments:
1
2
3
4
5
6
7
8
9
10
11
12
./myscript.sh one two three four five six seven eight nine ten
+ ./myscript.sh one two three four five six seven eight nine ten
The first argument is: one
The second argument is: two
The third argument is: three
The fourth argument is: four
The fifth argument is: five
The sixth argument is: six
The seventh argument is: seven
The eighth argument is: eight
The ninth argument is: nine
The tenth argument is: one0
The last line of the output shows that the tenth argument of the script was not replaced with the word ten, as you would expect.
Environment
Bitbucket Pipelines in Atlassian's infrastructure or with a self-hosted Linux Docker, Linux Shell, or MacOS runner.
Cause
This is standard behavior of the shell. Please see the Bash Reference Manual on Shell parameters, and then select the link Positional Parameters at the end of the page:
As stated in the Positional parameters page, the positional parameter N may be $N
when N
consists of a single digit. It must be referenced as ${N} when N is not a single digit.
When you use $10 in your bash script, bash will interpret it as ${1} followed by the string 0, which is why you see in the output the value of the first parameter followed by a 0.
Solution
If you pass 10 or more arguments to a script, you need to use ${N} instead of $N when N is not a single digit.
In the example shared above, you need to reference the tenth parameter as ${10} instead of $10.
It is recommended to use curly brackets around shell variables, ${N}, so that they are expanded correctly within the scripts and builds.
Was this helpful?