BASH vs GROOVY
BASH:
Double Quotes (
"..."
)Within double quotes, the following characters or sequences have special meanings:
$variable
: Variable substitution.$(command)
: Command substitution.\
(backslash): Escape the next character.command
: Another form of command substitution (though$()
is preferred because it's clearer and can be nested).
Examples:
bashCopy codename="Alice"
echo "Hello, $name!" # Outputs: Hello, Alice!
files=$(ls)
echo "Files: $files" # Outputs the list of files.
echo "This is a \"quote\"." # Outputs: This is a "quote".
date_today=`date`
echo "Today's date is: $date_today"
Single Quotes (
'...'
)Within single quotes, characters are not treated specially. Everything is taken literally.
Examples:
bashCopy codename="Alice" echo 'Hello, $name!' # Outputs: Hello, $name! echo 'This is a "quote".' # Outputs: This is a "quote". echo 'Files: $(ls)' # Outputs: Files: $(ls)
Groovy:
In Groovy, just as in Bash, there's a difference between single (
'...'
) and double ("..."
) quotes. Here's a breakdown of the differences with examples:Single Quotes (
'...'
)Literal String: Everything inside single quotes is taken literally in Groovy.
No string interpolation happens.
Example:
groovyCopy codedef name = 'Alice'
println 'Hello, $name!' // Outputs: Hello, $name!
Double Quotes (
"..."
)GString: If the string inside double quotes contains a
$
symbol followed by a variable name (or{expression}
), Groovy will substitute the value of that variable (or evaluate the expression) into the string.This mechanism is called string interpolation.
Example:
groovyCopy codedef name = "Alice"
println "Hello, $name!" // Outputs: Hello, Alice!
Triple Single Quotes (
'''...'''
)- Multiline String: This is a literal string that can span multiple lines.
Example:
groovyCopy codedef multiLine = '''This is
a multiline
string.'''
println multiLine
Triple Double Quotes (
"""..."""
)- Multiline GString: This is a string that can span multiple lines and supports string interpolation.
Example:
groovyCopy codedef name = "Alice"
def multiLineGString = """Hello,
$name! This is a multiline
GString."""
println multiLineGString
Difference between $var and ${var}
1. Bash:
$var
:
- Simply retrieves the value of the variable named
var
.
${var}
:
This also retrieves the value of the variable named
var
. However, the braces{}
allow for more complex operations or manipulations.Useful when you want to append something directly to the variable's value. For example, if you have a variable
a=hello
and you want to add "world" directly after it without a space,echo "$aworld"
would not work because Bash would try to find a variable namedaworld
. Instead, you'd useecho "${a}world"
.Allows for parameter expansion features like default values, string manipulation, etc.
Bash Examples:
bashCopy codea="hello"
echo "$aworld" # This would result in an error.
echo "${a}world" # This would print "helloworld".
2. Groovy:
$var
:
- It does string interpolation and retrieves the value of the variable named
var
.
${expression}
:
It interpolates the result of the evaluated expression inside the braces
{}
. The expression can be more than just a variable name; it can be a full Groovy expression.Useful for more complex inline evaluations within strings.
Groovy Examples:
groovyCopy codedef a = 5
def b = 10
println "Sum is: $a + $b = ${a + b}" // Outputs: "Sum is: 5 + 10 = 15"
In the above Groovy example, $a
and $b
interpolate the variable values, while ${a + b}
evaluates the expression a + b
and then interpolates its result.
In conclusion, while $var
is used for simple variable value interpolation in both Bash and Groovy, ${...}
is used for more advanced scenarios, like expressions in Groovy or string manipulations in Bash.
In jenkins pipeline what is the difference between sh """ and sh '''
In Jenkins pipelines, which typically use Groovy scripting, the difference between using double quotes ("""..."""
) and single quotes ('''...'''
) within the sh
step reflects the distinction between the handling of strings in Groovy.
Double Quotes (
"""..."""
):Interpolated String (GString): Groovy will interpolate (substitute) variable values within the string.
If you have Groovy variables that you want to be replaced with their values in the string, you'd use double quotes.
Variables or expressions inside
${...}
will be evaluated and the results will be inserted into the string.
Example:
groovyCopy codedef myVar = "world"
sh """
echo "Hello, ${myVar}!"
"""
This will output: Hello, world!
Single Quotes (
'''...'''
):Literal String: Groovy treats the content between single quotes as a literal string, meaning it won't perform any variable substitution.
This is useful when you want the string to be taken exactly as it is, without any interpolation.
Example:
groovyCopy codedef myVar = "world"
sh '''
echo "Hello, ${myVar}!"
'''
This will output: Hello, ${myVar}!
(instead of Hello, world!
)
So, when writing Jenkins pipelines, you'd choose between """..."""
and '''...'''
based on whether you want Groovy to interpolate variables within the shell script string. It's particularly important to be aware of this when writing shell scripts in the sh
step to ensure you're referencing variables as intended.
All possible ways of concatenating strings in bash and groovy
Concatenating strings is a common operation in both Bash and Groovy. Here are various ways you can concatenate strings in each:
Bash:
Using Double Quotes:
bashCopy codefirst="Hello" second="World" combined="$first $second" echo "$combined"
Using Single Quotes:
bashCopy codefirst='Hello' combined="$first"' World'" echo "$combined"
Using No Quotes:
bashCopy codefirst=Hello second=World combined=$first$second echo $combined
Using Braces for clarity:
bashCopy codepath="/usr" combined="${path}/bin:/sbin" echo $combined
Using the
+=
Operator:bashCopy codestring="Hello" string+=" World" echo $string
Groovy:
Using the
+
Operator:groovyCopy codeString first = "Hello" String second = "World" String combined = first + " " + second println(combined)
Using String Interpolation (GString):
groovyCopy codeString first = "Hello" String second = "World" String combined = "$first $second" println(combined)
Using the
<<
Operator:groovyCopy codeStringBuilder sb = new StringBuilder("Hello") sb << " " << "World" println(sb.toString())
Using
concat()
Method:groovyCopy codeString first = "Hello" String second = "World" String combined = first.concat(" ").concat(second) println(combined)
Using Multiple Strings in Parentheses:
groovyCopy codeString combined = ("Hello" " " "World") println(combined)
Using Triple Single or Double Quotes for Multi-line Strings:
groovyCopy codeString combined = '''Hello World''' println(combined)
OR
groovyCopy codeString combined = """Hello World""" println(combined)
These are the standard methods for concatenating strings in Bash and Groovy. Depending on the situation, one method might be more appropriate or readable than another.