Search code examples
bashshellvariablesconcatenation

Express variables within raw string in bash


Problem

I have a variable called boiler, and I want the variable si1 to be expressed, and I am unsure of how to do this in a simple and minimal fashion.

boiler='#!/bin/bash
source ../../functions.sh
current="${si1}"
ready custom
title
breadcrumbs \""$current"\" \"Options\"
# END OF BOILER (DO NOT REMOVE ABOVE CODE OR MODIFY IT)
'

ISSUE

The issue is that i want everything to be ignored withing this string (aka printed raw) except for the ${si1} variable.

EXPECTED OUTPUT

How could I concatenate the first part the variable and then the rest of the string while keeping it minimal and saving it back into the boiler variable?


Solution

  • You can delimit the string around ${si1}.

    boiler='#!/bin/bash
    source ../../functions.sh
    current='"${si1}"'
    ready custom
    title
    breadcrumbs \""$current"\" \"Options\"
    # END OF BOILER (DO NOT REMOVE ABOVE CODE OR MODIFY IT)
    '
    

    This is ordinary string concatenation. The strings delimited with ' will be literal, while the string delimited with " will have the variable expanded.

    Difference between single and double quotes in Bash