Skip to content

Quoting variable names properly

eroullit edited this page Jul 14, 2012 · 1 revision

It is possible to use variables within a test block. However, if the variable contains separators (white-spaces for instance), it must be properly quoted.

file_name="test file"

# This syntax would create 2 files: one called "test", one called "file"
test_expect_success "Create a file with a whitespace in its name" "
    touch $file_name
"
file_name="test file"

# Invalid test block syntax. The title would be evaluated as test pre-requisites
# and the test instruction would be evaluated as test title...
test_expect_success "Create a file with a whitespace in its name" "
    touch "$file_name"
"
file_name="test file"

# Escaping the variable with double quotes produces the right behavior.
test_expect_success "Create a file with a whitespace in its name" "
    touch \"$file_name\"
"
file_name="test file"

# Escaping the variable with single quotes produces the right behavior and looks cleaner.
test_expect_success "Create a file with a whitespace in its name" "
    touch '$file_name'
"
Clone this wiki locally