My script is supposed to take input from a pipe and replace newline characters with commas, using the bash's string substitution:

#! /bin/bash

read -d -r input 
echo $input 
echo $input

However, instead of replacing the newline character, bash tries to execute the first matching pattern:

echo -e "this\nis\na\ntest\n" | test.sh 

will give the following output:

test.sh: line 5: this: command not found 

and the variable $input is not changed. Double or single quotes did not help either. I'm using bash version 4.3.11 on Linux Mint.

1 Answer 1


Remove the $ from the assignment line inside the loop. You're trying to use the value as the name of the variable you're assigning to, which is causing Very Strange Behavior.

  • Using a second variable name resolves the problem. It's weird that you can't do this in bash. But why do I get the same problem with just the line ${input//\n/,} without any assignment?
    – Suzana
    May 27, 2015 at 18:54
  • 2
    Because at that point, you're trying to execute the variable's value, not echo it. You have to have an echo command in front of the variable if you're not assigning it (the modified value) to another variable.
    – John
    May 27, 2015 at 18:56

You must log in to answer this question.

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