touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2> /dev/null

where foo is a directory

It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' and putting a directory_ in front so the directory /home/test/foo would create a file directory_\home\test\foo

It does exactly what I want except a warning keeps printing tr: warning: an unescaped backslash at end of string is not portable

a. I don't know what that means b. I would like it to not show

I tried to redirect stderr to /dev/null but it doesn't work. How do I suppress it?

  • Please edit and clarify how you use this. Are you calling it as a function? Where is $1 defined? You should also be aware that while \ are allowed in file names, it is a Very Bad Idea® to include them and it will lead to all sorts of trouble later on.
    – terdon
    Commented Aug 6, 2014 at 14:37
  • It's in a script. to run it I "sh script nameOfDirectory" Commented Aug 6, 2014 at 14:38
  • 3
    Please edit your question to clarify accordingly. I also strongly suggest you consider using any other character but \ , backslashes are special as you're finding out.
    – terdon
    Commented Aug 6, 2014 at 14:39

3 Answers 3


The error message means that you have an unescaped backslash. In the *nix world, special characters are 'escaped' by adding a backslash before them. That means that they're interpreted as simple characters and not special ones. For example:

$ printf 'foo\nbar'

The \n is a special character that means newline. But if we escape the slash we get:

$ printf 'foo\\nbar'

So, you can get rid of it either by escaping the backslash:

touch ~/deleted/"$(echo "directory_$(readlink -f -- "$1")"|tr '/' '\\')

Or by redirecting the error output of tr (not of touch which is what you were doing):

touch ~/deleted/"$(echo "directory_$(readlink -f -- "$1")"|tr '/' '\' 2> /dev/null)

However, I cannot stress enough that it is a Very Bad Idea® to create file names with backslashes. Really. They will be hell to parse and deal with later on. Why not use another character?

  • Maybe the OP from windows environment?
    – cuonglm
    Commented Aug 6, 2014 at 15:16
  • @Gnouc yes, which means it's an even worse idea to use `\`.
    – terdon
    Commented Aug 6, 2014 at 15:18
  • Git Bash in Windows 10 complains about [...] | tr '\\' '/' although the backslash is properly escaped. Go figure.
    – fbiville
    Commented Jan 23, 2019 at 16:58
  • @Rolf Windows uses `` as the path delimiter, so that might cause issues there.
    – terdon
    Commented Jan 23, 2019 at 17:19

Some expansions happen before redirection so you have to place it directly against your tr command:

touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\' 2> /dev/null)

Use single quotes with double back slash, and you will not get any error.

touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\\')
  • Fun thing is, if url=D:\Programs\Git\mingw64/share/doc/git-doc/git-log.html then ``url1=echo $url | tr '\\\ '/'``` shows the warning, while url1=$(echo $url | tr '\\' '/')` does not.
    – ThomasH
    Commented Dec 19, 2018 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .