---
title: "__BeanShell"
description: "JMeter __BeanShell function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values."
url: https://docs.jmeter.ai/functions/beanshell/
lastUpdated: 2026-10-01
source: docs.jmeter.ai
---

# __BeanShell

*Also documented in context in the [full Functions and Variables reference](/user-manual/functions/#__BeanShell).*

The BeanShell function evaluates the script passed to it, and returns the result.

> **Note**
> For performance it is better to use __groovy function

**For full details on using BeanShell, please see the BeanShell web-site at [http://www.beanshell.org/](http://www.beanshell.org/)**

> **Note**
> Note that a different Interpreter is used for each independent occurrence of the function
> in a test script, but the same Interpreter is used for subsequent invocations.
> This means that variables persist across calls to the function.

A single instance of a function may be called from multiple threads.
However the function `execute()` method is synchronised.

If the property “`beanshell.function.init`” is defined, it is passed to the Interpreter
as the name of a sourced file. This can be used to define common methods and variables. There is a
sample init file in the bin directory: `BeanShellFunction.bshrc`.

The following variables are set before the script is executed:

- `log` - the [Logger](https://www.slf4j.org/api/org/slf4j/Logger.html) for the BeanShell function (*)
- `ctx` - [JMeterContext](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html) object
- `vars` - [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html) object
- `props` - JMeterProperties (class [`java.util.Properties`](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)) object
- `threadName` - the threadName (String)
- `Sampler` - the current [Sampler](https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html), if any
- `SampleResult` - the current [SampleResult](https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html), if any

(*) means that this is set before the init file, if any, is processed.
Other variables vary from invocation to invocation.

| Name | Required | Description |
| --- | --- | --- |
| BeanShell script | Yes | A beanshell script (not a file name) |
| Name of variable | No | A reference name for reusing the value                computed by this function. |

Example:

**`\$\{__BeanShell(123*456)\}`**
: returns `56088`

**`\$\{__BeanShell(source("function.bsh"))\}`**
: processes the script in `function.bsh`

> **Note**
> Remember to include any necessary quotes for text strings and JMeter variables that represent text strings.

## Related

- [Functions and Variables Guide](/topics/functions-and-variables/)
- [Regex Extractor Builder](/tools/regex-tester/)
- [Full Functions Reference](/user-manual/functions/)
- [Component Reference](/user-manual/component-reference/)
