简体   繁体   中英

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

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)

You should use something like this

hps_dataset="--dataset_config \"$DATASET_CONFIG\" --dataset_worker_num 16"

Also for the future variables in bash are all caps like HPS_DATASET .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM