Search code examples
pythonpython-3.xshellgflags

Passing shell variables containing whitespace as argument


I have a dynamic path in the variable DATASET_CONFIG

This is a small code to demonstrate the problem

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
RUN_SCRIPT="$SCRIPT_DIR/file.py"
DATASET_CONFIG="$SCRIPT_DIR/../dataset_config/ffhq.json"
hps_dataset="--dataset_config $DATASET_CONFIG --dataset_worker_num 16"
python_version="python3"

$python_version "$RUN_SCRIPT" \
$hps_dataset \
;

As you can see I have used "$RUN_SCRIPT" instead of $RUN_SCRIPT because SCRIPT_DIR contain whitespace but I cannot do the same for $hps_dataset


Solution

  • You need to use an array to store the dataset. I'd also recommend you stop using ALLCAPS varnames (here's why):

    script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
    run_script="$script_dir/file.py"
    dataset_config="$script_dir/../dataset_config/ffhq.json"
    hps_dataset=( --dataset_config "$dataset_config" --dataset_worker_num 16 )
    python_version="python3"
    
    "$python_version" "$run_script" "${hps_dataset[@]}"
    

    Use all the quotes shown here.

    Because we're using an array, you cannot use /bin/sh to run the script. You'll have to explicitly use bash or ksh (or perhaps zsh)