"ls" works in cmd line but not in script for directories with space in their names "ls" works in cmd line but not in script for directories with space in their names shell shell

"ls" works in cmd line but not in script for directories with space in their names


There are two problems with your script. First, you are not setting VARDIR correctly as you have too many backslashes. Second, you should put quotes around any use of any variable.

$ cat test.sh#!/bin/bashVARDIR="testfolder/folder01"ls "$VARDIR"VARDIR="testfolder/folder01 (copy)"ls "$VARDIR"

When setting VARDIR, you can either use backslashes, or quotes, but not both:

VARDIR="testfolder/folder01 (copy)"

or

VARDIR=testfolder/folder01\ \(copy\)


Try:

ls "$VARDIR"

The double quotes will preserve the space, no need for backslashes.