I want to create a file named ./https://www.example.com, but the following doesn't work:

[ ~]$ URL="https://www.example.com"
[ ~]$ echo "Hello!" >"$URL"
bash: https://www.example.com: No such file or directory

How can I tell bash I want a filename with slashes in it, and that I'm not trying to look into a different (nonexistent) directory?

  • 8
    You can't. The slash is one of only two characters that are not allowed in Unix file names. (The other is NUL.). – John1024 Apr 2 '15 at 1:44
  • 2
    I just tried it on Ubuntu 12.04 and that allows for back-slashes in file names... So the real question is why one would want a file with a name that is a URL, and if that reason is trivial, one could easily substitute the forward slashes with backslashes... – Louis Parkin Apr 2 '15 at 14:06

This doesn't make URL a file, it makes it a variable (via assignment):

$ URL="https://www.example.com"


Can you explain more about what you're trying to do?

  • 1
    Yes, $URL is a variable. When you execute command [args] >filedescriptor, filedescriptor is taken as the name of the file. E.g. echo hello, world! > hello.txt will create a file with the name hello.txt and put "hello,world!" in it. In this case, I wanted a file whose name was https://www.example.com. However, as @John1024 points out, this is not possible because "/" is not a valid character in a filename. – nullUser Apr 6 '15 at 23:03

I think you're going to remap certain special characters, because '/' just can't be in a valid POSIX filename.

Try this:

FILENAME="$(echo "$URL" | tr '//:?+' '_')"
echo "Hello!" >"$FILENAME"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.