I have a script that has a string in a file name like so:
filename_with_spaces="a file with spaces"
echo test > "$filename_with_spaces"
test_expect_success "test1: filename with spaces" "
run cat \"$filename_with_spaces\"
run grep test \"$filename_with_spaces\"
"
test_expect_success is defined as:
test_expect_success () {
echo "expecting success: $1"
eval "$2"
}
and run is defined as:
#!/bin/zsh
# make nice filename removing special characters, replace space with _
filename=`echo $@ | tr ' ' _ | tr -cd 'a-zA-Z0-9_.'`.run
echo "#!/bin/zsh" > $filename
print "$@" >> $filename
chmod +x $filename
./$filename
But when I run the toplevel script test_expect_success… I get cat_a_file_with_spaces.run with:
#!/bin/zsh
cat a file with spaces
The problem is the quotes around a file with spaces in cat_a_file_with_spaces.run is missing. How do you get Z shell to keep the correct quoting?
Thanks
Try
. It is what (q) modifier was written for. Same for run script:
. And it is not bash, you don’t need to put quotes around variables: unless you specify some option (don’t remember which exactly)
always passes exactly one argument to
commandno matter what is in$var. To ensure that some zsh option will not alter the behavior, putat the top of every script.
Note that initial variant (
run cat \"$filename_with_spaces\") is not a correct quoting: filename may contain any character except NULL and/used for separating directories.${(q)}takes care about it.Update: I would have written test_expect_success function in the following fashion:
Usage: