Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash quote behavior and sed

I wrote a short bash script that is supposed to strip the leading tabs/spaces from a string:

#!/bin/bash
RGX='s/^[ \t]*//'
SED="sed '$RGX'"
echo "       string" | $SED

It works from the command line, but the script gets this error:

sed: -e expression #1, char 1: unknown command: `''

My guess is that something is wrong with the quotes, but I'm not sure what.

like image 866
krs1 Avatar asked Oct 24 '25 02:10

krs1


1 Answers

Putting commands into variables and getting them back out intact is hard, because quoting doesn't work the way you expect (see BashFAQ #050, "I'm trying to put a command in a variable, but the complex cases always fail!"). There are several ways to deal with this:

1) Don't do it unless you really need to. Seriously, unless you have a good reason to put your command in a variable first, just execute it and don't deal with this messiness.

2) Don't use eval unless you really really really need to. eval has a well-deserved reputation as a source of nasty and obscure bugs. They can be avoided if you understand them well enough and take the necessary precautions to avert them, but this should really be a last resort.

3) If you really must define a command at one point and use it later, either define it as a function or an array. Here's how to do it with a function:

RGX='s/^[ \t]*//'
SEDCMD() { sed "$RGX"; }
echo "       string" | SEDCMD

Here's the array version:

RGX='s/^[ \t]*//'
SEDCMD=(sed "$RGX")
echo "       string" | "${SEDCMD[@]}"

The idiom "${SEDCMD[@]}" lets you expand an array, keeping each element a separate word, without any of the problems you're having.

like image 101
Gordon Davisson Avatar answered Oct 26 '25 18:10

Gordon Davisson