Check assign

I have a person script which has a following statement.

BUILD_FOLDER=$2
i=$((${#BUILD_FOLDER}-1))

if [ "${BUILD_FOLDER:$i:1}" != "/" ]
then
     BUILD_FOLDER=$BUILD_FOLDER/
     #echo $BUILD_FOLDER

else
     echo "  "
     #echo $BUILD_FOLDER
fi

What and how this statement works ?

i=$((${#BUILD_FOLDER}-1))

It means assign the length of the variable BUILD_FOLDER minus 1 to variable i

For example:

$ BUILD_FOLDER=foobarbaz             # the length of the variable is 9
$ echo $((${#BUILD_FOLDER}-1))
8

Hi,

$# Number of arguments supplied. Here ${#BUILD_FOLDER} gives length of the fields given as a input in $2

(( )) form is for arithmetic expansion.

For ex. (( i++ ))

I will try with example as follows:

$ cat 3.sh 
#!/bin/bash

BUILD_FOLDER=$2
i=$((${#BUILD_FOLDER}-1))
j=0 # Not required 
echo $i
((  j++ ))
echo "j is: "$j
$ /3.sh hi "how are you"
10
j is: 1
$ ./3.sh hi how are you
2
j is: 1
i=$((${#BUILD_FOLDER}-1))

${#BUILD_FOLDER} is the length (number of characters) of the variable BUILD_FOLDER

$(( xxx -1)) is xxx minus one

The resulting value is affected to i which then contains the offset of the last character of the string $BUILD_FOLDER

If you don't really need to sometimes print a line just containing two spaces, you could replace everything you showed us in post #1 with just:

BUILD_FOLDER=${2%/}/

which strips off a trailing slash from the contents of $2 if and only if there was one there, and then adds a trailing slash.

Thank you so much :slight_smile: