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
run cat ${(q)filename_with_spaces}
. It is what (q) modifier was written for. Same for run script:
echo -E ${(q)@} >> $filename
. And it is not bash, you don't need to put quotes around variables: unless you specify some option (don't remember which exactly)
command $var
always passes exactly one argument to command
no matter what is in $var
. To ensure that some zsh option will not alter the behavior, put
emulate -L zsh
at 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:
function test_expect_success()
{
emulate -L zsh
echo "Expecting success: $1" ; shift
$@
}
Usage:
test_expect_success "Message" run cat $filename_with_spaces
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With