How to mount partition with spaces in path

I created one partition and I wanted to mount that partition to this directory /home/max/VirtualBox VMs

I wrote this line in fstab:

/dev/sda4    /home/max/VirtualBox\ VMs  ext4    defaults        0 0

but it's giving this error

[mntent]: line 16 in /etc/fstab is bad

I know I am getting this error because of space in between 'virtualBox VMs'

Is it possible to mount to that Directory?

[max@localhost VirtualBox VMs]$ pwd
/home/max/VirtualBox VMs

Solution 1:

Use quote marks.

/dev/sda4 "/home/max/VirtualBox VMs" ext4 defaults 0 0

Solution 2:

fstab has its own syntax. To use spaces as part of a directory name, you have to specify its code point as a zero-padded 3-digit octal number, preceded by a backslash (escape character).

In ASCII, the space character's code point is 32 or 40 in octal, so you can use:

/dev/sda4               /home/max/VirtualBox\040VMs  ext4    defaults        0 0

Note that, while code points are supported for other characters as well, the support is rather flaky. On my machine, you can write \127 instead of W, but not \070 instead of 8...

Solution 3:

I am converting the whole path to code point with a Bash function:

fstab_path(){
    local path=$1
    local s=
    local c=
    for i in $(seq 1 ${#path})
    do
        c=${path:i-1:1}
        s="$s"$(printf '\\0%o' "'$c")
    done
    echo "$s"  >/dev/stdout
}

path="path with spaces tabs etc.."
fpath=$(fstab_path "$path")