Home > Back-end >  Use bash variable in substitution command with this formatting, with JSON
Use bash variable in substitution command with this formatting, with JSON

Time:11-24

I have a bash script where one of the part in a command changes from time to time.
So I tried to change the script, so we could ask for it, or change at one part only, etc., but can't really make it.

If I write this, it works:

#!/bin/bash
changing_stuff='"Active-2021-xy Part YX"'
total_number=`Command_xy show base name "Active-2021-xy-yz Part YX" limit 1 --format json | jq '.total'`


I've used '" "' because as you see in the original command it requires " " for that part.
How could I add the changing_stuff into the middle of the script?
Thanks a lot!

CodePudding user response:

The following should work. There's no need to add quotes into your changing_stuff variable. Putting quotes around the variable when you use it causes the whole value (including the spaces) to be passed as a single argument to Command_xy.

#!/bin/bash
changing_stuff='Active-2021-xy Part YX'
total_number=`Command_xy show base name "$changing_stuff" limit 1 --format json | jq '.total'`

CodePudding user response:

You seem to be looking for the trivial

#!/bin/bash
changing_stuff='Active-2021-xy Part YX'
total_number=`Command_xy show base name "$changing_stuff" limit 1 --format json | jq '.total'`

The quotes are simply a mechanism for keeping the string with spaces in it as a single argument, in both places.

(Tangentially, you also want to replace the backticks with modern command substitution syntax:)

#!/bin/bash
changing_stuff='Active-2021-xy Part YX'
total_number=$(Command_xy show base name "$changing_stuff" limit 1 --format json | jq '.total')
  • Related