Bash – How to suppress stderr warning messages from a command inside command substitution

bashcommand-substitutionstderr

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?

Best Answer

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)