I have SED patterns:


/b\./s/.*c\.. \([^ ]*\) .*/\1/p

and so on.

I need to pass these to an echo command as variables.

At the moment, I define the $pattern variable like so:


and then pipe it to echo, like so:

echo "$OUTPUT" | sed "s/$pattern/g"

But the code is not passing the pattern, but a command and returns the error

=[^a-zA-Z0-9]: command not found

What's going wrong?

  • Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works... – Zip Dec 7 '17 at 16:55
  • see also: unix.stackexchange.com/q/32907/117549 – Jeff Schaller Dec 7 '17 at 17:00
  • [^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/\1/p is a full sed script. – Stéphane Chazelas Dec 7 '17 at 17:18
  • 1
    Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error. – PesaThe Dec 7 '17 at 17:57
$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"


Shell variables are assigned without a leading $.






Shell variables are assigned without a leading $.

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.