I have a particular string loaded into a variable (v1). I want to define a new variable equal to this variable MINUS the new line and carriage returns so thus I need to delete the new line and carriage returns from this string assigning my new variable.

Here's an example of what I'm trying to do (--> means the terminal output):


v2=  $(echo $v1 | tr -d '/n')  #This yields the error below.
--> bash: Hello\nThere: No such file or directory

Likewise, if I try to replace say the "l" instead of the '/n', I get:

v2=  $(echo $v1 | tr -d l)
--> bash: Heo\nThere: No such file or directory 

Notice the "l" disappears on the second example, but this "No such file or directory" pops up which I have no idea what it's signifying. What does this indicate in this context? What file or directory is it looking for? I am just piping the results from echo into tr. I'm not sure what it's referring to when it's talking about No such file or directory.

Moreover, it works if I just do it like so without assigning a new variable

echo $v1 | tr -d l
-->   Heo\nThere

Am I assigning the new variable correctly here? I am using the format VariableName= $(expression)


For one, this sets v1 to the string Hello\nThere literally, with a backslash in the middle. To include a literal newline, you'd use (in Bash/Ksh/Zsh):


or (POSIXly):


Then, this:

v2=  $(echo $v1 | tr -d '/n') 

is similar to v2= somecommand, which is similar to v2=value somecommand, i.e. it runs a command with v2 set in the environment of the command. With the line you have, it expands the command substitution, wordsplits and globs it, gets Hello\nThere as the first and only word, and tries to run that as a command (with v2 set to the empty string in its environment).

That fails, since you don't have Hello\nThere in the path, hence the error message saying Hello\nThere: No such file or directory.

You can't use whitespace in shell variable assignments like that, you need to use v2=$(...). Also, you should put quotes around the variable expansion, and you probably meant to use \n in the argument to tr, and also \r to remove carriage returns too:

So, e.g. with Bash, this would print "Hellothere":

v2=$(echo "$v1" | tr -d '\n\r')
printf "%s\n" "$v2"

Though if we can use $'', we can probably also use the ${var/pattern/replacement} expansion to remove the newlines and carriage returns:

printf "%s\n" "$v2"

(Double slash to replace all matches, and without the replacement part the matches are just removed. Note the brackets [] should not be quoted, since we want them to retain their pattern match meaning, but the \n\r inside must be within $'' to be taken as NL and CR, and not just backslash and letters.)



The commands you want are these:


v2=$(echo $v1 | tr -d '\\n')

With the way that you have it, \n is being interpreted as a literal \n and not as a newline character and that causes the error. Using \\n takes care of this.

You can verify afterwards:

echo $v2


  • The problem was actually that I had an extra space before the variable assignment (bash!). The '/n' works and if you see the other answer it has just '\n' instead of '\\n'. Why do you think it has to be '\\n'? He has v2=$(echo "$v1" | tr -d '\n\r')
    – Prospero
    Jun 16 at 22:51
  • @Prospero When I ran it your way, I got the error. \\n didn't give the error. When running your command with /n, the output is Hello\There which isn't what you want. Jun 16 at 23:16
  • I'm using just the one backslash and it's working now and that's what the other answerer said as well. Are you sure you don't also have that extra space in it?
    – Prospero
    Jun 16 at 23:18
  • @Prospero No. It's exactly as I typed it. The other answer has the space in the same place. Jun 16 at 23:28

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.