Home > front end >  How to execute bash variable with double quotes and get the output in realtime
How to execute bash variable with double quotes and get the output in realtime

Time:05-25

I have a variable that has a command that I want to run. It has a bunch of double-quotes. when I echo it, it looks beautiful.

I can copy-paste it and run it just fine.

I tried simply $cmd, but it doesn't work. I get an error as if the command is malformed.

I then tried running it via eval "$cmd" or similarly, bash -c "$cmd", which works, but I don't get any output until the command is done running.

Example with bash -c "$cmd":

This runs the command, BUT I don't get any output until the command is done running, which sucks and I'm trying to fix that:

cmd="docker run -v \"$PROJECT_DIR\":\"$PROJECT_DIR\" \
    -v \"$PPI_ROOT_DIR/utilities/build_deploy/terraform/modules/\":/ppi_modules \
    --workdir \"$PROJECT_DIR/terraform\" \
    --env TF_VAR_aws_account_id=$AWS_ACCOUNT_ID \
    --env TF_VAR_environment=${ENVIRONMENT} \
    --env TF_VAR_region=${AWS_DEFAULT_REGION:-us-west-2} \
    ${OPTIONAL_AWS_ENV_VARS} \
    ${CUSTOM_TF_VARS} \
    ${TERRAFORM_BASE_IMAGE} \
    init --plugin-dir=/.terraform/providers \
        -reconfigure \
        -backend-config=\"bucket=${AWS_ACCOUNT_ID}-tf-remote-state\" \
        -backend-config=\"key=${ENVIRONMENT}/${PROJECT_NAME}\" \
        -backend-config=\"region=us-west-2\" \
        -backend-config=\"dynamodb_table=terraform-locks\" \
        -backend=true"
# command output looks good. I can copy and paste it and run it my terminal too.
echo $cmd
# Running the command via bash works, 
# but I don't get the output until the command is done running, 
# which is what I'm trying to fix:
bash -c "$cmd"

Here is an example using bash array. It prints it to screen perfectly, but just like running it like $cmd, it throws an error as if the command is malformed:

cmd=(docker run -v \"$PROJECT_DIR\":\"$PROJECT_DIR\" \
    -v \"$PPI_ROOT_DIR/utilities/build_deploy/terraform/modules/\":/ppi_modules \
    --workdir \"$PROJECT_DIR/terraform\" \
    --env TF_VAR_aws_account_id=$AWS_ACCOUNT_ID \
    --env TF_VAR_environment=${ENVIRONMENT} \
    --env TF_VAR_region=${AWS_DEFAULT_REGION:-us-west-2} \
    ${OPTIONAL_AWS_ENV_VARS} \
    ${CUSTOM_TF_VARS} \
    ${TERRAFORM_BASE_IMAGE} \
    init --plugin-dir=/.terraform/providers \
        -reconfigure \
        -backend-config=\"bucket=${AWS_ACCOUNT_ID}-tf-remote-state\" \
        -backend-config=\"key=${ENVIRONMENT}/${PROJECT_NAME}\" \
        -backend-config=\"region=us-west-2\" \
        -backend-config=\"dynamodb_table=terraform-locks\" \
        -backend=true)

echo "${cmd[@]}"

"${cmd[@]}"

How can I execute a bash variable that has double-quotes, but run it so I get the output in realtime, just as if I executed via $cmd (which doesn't work)

Similar to these questions, but my question is to run it AND get the output in realtime:

Execute command containing quotes from shell variable

Bash inserting quotes into string before execution

bash script execute command with double quotes, single quotes and spaces

CodePudding user response:

In your array version, double quotes escaped by a backslash become part of the arguments, which is not intended.

So removing backslashes should fix the issue.

  •  Tags:  
  • bash
  • Related