Using $variable in shortcode

If you’re using PHP > 5.3, then you can use a closure on the the_content filter. This filter needs to be added after the $variable has been defined and before the the_content filter has fired.

add_filter( 'the_content', function( $content ) use ( $variable ) {
  return str_replace( '[variable][/variable]', $variable, $content );
} );

Shortcodes are process by core on the_content hook at a priority of 11. So any priority 10 or less will be run before that. If you want the callback to be run before wpautop, use a priority less than 10.

There’s no reason to add_shortcode() because this code replaces the shortcode with the variable before do_shortcode() is run.

Filters should ideally be placed in the themes functions.php file, but if for some reason $variable isn’t available to functions.php, then this little hack should work.