0

I'm trying to execute the following printf statement in a zsh script I'm working on:

printf "%s\n" "${CREATE_WS[@]}"

Where $CREATE_WS is an indexed array as follows:

CREATE_WS=("************************************************************************" \
        "You are about to create the $workspace workspace using the" \
        "version set $versionset. Do you want to continue?" \
        "************************************************************************" \
        " ")

Where $workspace and $versionset are taken from command line arguments. If I echo both variables by themselves, the console shows that they are non-null and have the string that was entered on the command line. However, when the printf statement runs, the variables do not expand into the strings that they contain as expected, and are printed as though they are null.

I've tried wrapping the variables in curly braces like so: ${versionset}. But this also doesn't give me the desired behavior. What have I written wrong in this procedure, and how do I correct it?

12
  • 2
    Variables inside strings are expanded when you assign to CREATE_WS, not when you use it. Commented Jul 2, 2021 at 16:53
  • Were the $workspace and $versionset variables set when you executed the CREATE_WS= statement? Commented Jul 2, 2021 at 16:54
  • Good point. CREATE_WS is a constant that is set at runtime. Since that gets set PRIOR to the script processing command line arguments, those two variables aren't set yet at that point. Commented Jul 2, 2021 at 16:56
  • All variables are set at runtime. You just need to fix the order that they're set. Commented Jul 2, 2021 at 16:57
  • 1
    It sounds like you should write a function, not use variables. Commented Jul 2, 2021 at 19:18

1 Answer 1

1

Try this (note the single quotes):

create_ws_msg=(
  'workspace: $workspace'
  'version: $versionset'
  '***'
  '' )

workspace=ws21
versionset=3.14

print ${(eF)create_ws_msg}

# => workspace: ws21
# => version: 3.14
# => ***
# => 

The variable reference ${...} in the print statement has two parameter expansion flags, in parentheses. e expands nested variables, and F adds newlines. The zshexpn man page has details on these and other flags.


As noted in the comments, you could use a function instead. That may prove easier for a future maintainer to understand, since zsh uses the same syntax as bash.

Using named variables:

function print_create_ws_msg {
  printf '%s\n' \
    "workspace: $workspace" \
    "version: $versionset" \
    '***'
}

workspace='ws42'
versionset='4.2'
print_create_ws_msg

Using positional parameters:

function print_create_ws_msg {
  printf '%s\n' \
    "workspace: $1" \
    "version: $2" \
    '***'
}

print_create_ws_msg 'wsa' '99'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.