I have a script where I dinamically change the arguments which must be passed to a command (mkvpropedit in this case). Consider the example script below:
#!/bin/bash
LANG_NAME="eng lish"
MYSETTINGS=()
MYSETTINGS+=("--edit 1")
MYSETTINGS+=("--set \"language=${LANG_NAME}\"")
echo "Count = ${#MYSETTINGS[@]}" # should be 2
set -x # enable to see the invoked command
mkvpropedit ${MYSETTINGS[@]}
When I run this, I get in the console:
[~] # ./test.sh
Count = 2
+ mkvpropedit --edit 1 --set '"language=eng' 'lish"'
But I would like not having the single quotes on the final invocation of mkvpropedit, like so:
+ mkvpropedit --edit 1 --set "language=eng lish"
I tried also echoing the array into a variable, and echo removes the single quote, but then I'm not able to use the variable as an argument of mkvpropedit because the single quotes appear again...
Of course the script has to work also if the variable is a single word, such as LANG_NAME="eng"
. My Bash version is 3.2 (Busybox, actually).
Updated question
Probably the example below better explains what I'm trying to do. I've changed some names to be replicable.
#!/bin/bash
TITLE_NAME="my title"
MYSETTINGS=()
MYSETTINGS+=("--edit track:2")
MYSETTINGS+=("--set "name=${TITLE_NAME}"")
set -x
mkvpropedit file.mkv ${MYSETTINGS[@]}
If I run this script, I get (due to the wrong quote):
# ./test.sh
+ mkvpropedit file.mkv --edit track:2 --set '"name=my' 'title"'
Error: More than one file name has been given ('file.mkv' and 'title"').
While if I run, manually:
# mkvpropedit file.mkv --edit track:2 --set "name=my title"
The file is being analyzed.
The changes are written to the file.
Done.
So it's definitely a quoting issue; I would like to invoke mkvpropedit using the array in the script.
Using eval
What seems to work, at the moment, is inserting mkvpropedit
and file.mkv
into the array and eventually call eval "${MYSETTINGS[@]}"
, but is it worth and safe? Isn't eval
evil (pun intended)?
TITLE_NAME="my title"
MYSETTINGS=(mkvpropedit file.mkv)
MYSETTINGS+=("--edit track:2")
MYSETTINGS+=("--set "name=${TITLE_NAME}"")
set -x
eval "${MYSETTINGS[@]}"
Returns:
# ./test.sh
+ eval mkvpropedit file.mkv '--edit track:2' '--set "name=my title"'
++ mkvpropedit file.mkv --edit track:2 --set 'name=my title'
The file is being analyzed.
The changes are written to the file.
Done.
echo -n my title | xxd
(e280 af
) – nezabudka Aug 12 '21 at 17:39