Frage

Wie sollte repariert werden? command Variable, um ein korrektes Verhalten zu erhalten?

#!/bin/bash

function f ( )
{
    echo "$2"
}
command="f --option=\"One Two Three\" --another_option=\"Four Five Six\""
$command

f --option="One Two Three" --another_option="Four Five Six"

Das erste Anruf ist falsch, zweitens - richtig

$> ./test.sh 
Two
--another_option=Four Five Six
War es hilfreich?

Lösung

BASH -FAQ -Eintrag Nr. 50: "Ich versuche, einen Befehl in eine Variable zu bringen, aber die komplexen Fälle scheitern immer!"

TL; DR: Verwenden Sie ein Array.

command=(f --option="One Two Three" --another_option="Four Five Six")
"${command[@]}"

Andere Tipps

Sie können die Variable nicht beheben. Doch kannst du:

eval $command
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top