Bash quotes in a variable processed differently when expanded to a command

Explaining the issue using examples ...

Demonstrates that single quotes after -chapters become escaped when the variable expands (I did not expect this):

prompt@ubuntu:/my/scripts$ cat test1.sh
#!/bin/bash
actions="--tags all:"
actions+=" --chapters ''"
mkvpropedit "$1" $actions

prompt@ubuntu:/my/scripts$ ./test1.sh some.mkv
Error: Could not open '''' for reading.

And now for some reason, mkvpropedit gets double quotes as part of the file name (I didn't expect this either):

prompt@ubuntu:/my/scripts$ cat test1x.sh
#!/bin/bash
command="mkvpropedit \"$1\""
command+=" --tags all:"
command+=" --chapters ''"
echo "$command"
$command

prompt@ubuntu:/my/scripts$ ./test1x.sh some.mkv
mkvpropedit "some.mkv" --tags all: --chapters ''
Error: Could not open '''' for reading.

The above echo'd command seems correct. Putting the same text in another script gives the expected result:

prompt@ubuntu:/my/scripts$ cat test2.sh
#!/bin/bash
mkvpropedit "$1" --tags all: --chapters ''

prompt@ubuntu:/my/scripts$ ./test2.sh some.mkv
The file is being analyzed.
The changes are written to the file.
Done.

Can someone explain why quotation marks do not behave as expected. I found that searching for this issue is difficult because there are so many other quotes on the Internet. I don’t even know how to explain the question without examples.

, - - , , , , . , - script . , .

.

+4
1

, , . , ($foo) ($(cmd args)), . [ 1]

glob. / ( ). [ 2]

, bash $args ,

cmd $args

$args , . $args , .

- $IFS ; $args . , , , , , , . .

: bash .

$args , :

cmd "${args[@]}"

$args , .

, :

actions=(--tags all:)
actions+=(--chapters '')
mkvpropedit "$1" "${actions[@]}"

, , . :

args=("$1")
args+=(--tags)
args+=(all:)
args+=(--chapters)
args+=('')
mkvpropedit "${args[@]}"

command=(mkvpropedit "$1" --tags all: --chapters '')
"${command[@]}"

, -.

man bash ( -) , bash , "". .


:

  • eval bash -c, . , .

  • - , " ", , . -, - $IFS, . , . , .

+6

All Articles