Pete Lensky
Pete Lensky

Reputation: 23

Using variables in terminal

I wrote this code:

cat /etc/passwd | cut -d : -f1 | sed -n "${FT_LINE1}, ${FT_LINE2} p"

Output:

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

But I have a problem with variables $FT_LINE1, $FT_LINE2.
When I use constants instead of a variables, this code works correctly

cat /etc/passwd | cut -d : -f1 | sed -n "3, 5 p"

I tried to use these constructions:

sed -n -e "${FT_LINE1}, ${FT_LINE2} p"
sed -n "{$FT_LINE1}, {$FT_LINE2} p"
sed -n "${FT_LINE1},${FT_LINE2} p"
sed -n "${FT_LINE1}, ${FT_LINE2}" p
sed -n "$FT_LINE1, $FT_LINE2" p

but the error remained.

Upvotes: 1

Views: 602

Answers (1)

agc
agc

Reputation: 8406

As noted in melpomene and PesaThe's comments, sed address ranges can't be blank, both shell variables ${FT_LINE1}, and ${FT_LINE2}, must be set to some appropriate value.

This simplest way to reproduce the error is:

sed ,

Which outputs:

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

Because , is not a sed command, it's just a delimiter that separates range addresses.

It might help to look at some other related errors. Let's add a starting address of 1:

sed 1,

Output:

sed: -e expression #1, char 2: unexpected `,'

Which seems unhelpful, since it should be expecting an address after the ,. Now let's add a second address of 1:

sed 1,1

Output:

sed: -e expression #1, char 3: missing command

A little better, but really it's char 4 that's missing a command, or rather there's a missing command after char 3.

Now let's add a command, and a bit of input and it works:

echo foo | sed 1,1p

Output:

foo

Upvotes: 1

Related Questions