1

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 
$input=${input//\n/,}
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.

2

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 '15 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 '15 at 18:56

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.