That would likely be a "display" thing with ls
, i.e., if you do ls -p
or ls -F
, it should consistently add the slash after directories. It's essentially adding visual cues as to what it thinks the file is. (Do note that -F
will also, e.g., append @
to symlinks, *
to files with executable permissions, |
to named pipes, =
to sockets and so on). You can also reproduce with ls -d directory/
vs ls -d directory
.
/
and \0
I believe is the only two characters not valid in a file/directory name.
Always adding the slash should be safe, if you've assigned mydirectory and myfile correctly, doing vim "$mydirectory/$myfile"
should do what you expect. (With the caveat of $mydirectory
and $myfile
having been assigned correctly). foo/bar
and foo//bar
should be equivalent.
Generally; I really dislike assigning the output of ls to variables and dealing with it, primarily because I've previously been surprised about characters ending up into filenames combined with lacking quoting, ending up with destructive results. (I.e. prefix=/home user=" oops" rm -rf $prefix$user
, which would end up recursively deleting /home
, and then trying to recursively delete ./oops
).
Instead of using ls, I try when I can to rely on glob expansion by bash, or find -print0 | xargs -0
. If you do use ls
, ensure you're NOT using an existing alias for ls
, and you use the display options that fits your particular task.
\
. This is a forward slash:/
(or just slash). – cjm Jul 15 '15 at 20:13/
ever: there are only 2 banned characters in Unix file-name (/
and null). However sometimes a command likels -F
may add a/
to the end of directory names. Sometimes you may choose to add them to the end of directory names to avoid ambiguity. E.g to dereference a symlink to directory. – ctrl-alt-delor Jul 15 '15 at 21:47