Vasyl Kozhushko
Vasyl Kozhushko

Reputation: 83

Using variables in command line

I wrote a code and it works great but I need to use variables instead static numbers for scopes 8 and 16

cat /etc/passwd | sed '/^#/d' | sed -n 'n;p' | sed 's/:\(.*\) //g' | sed 's/ /,/g' | sed 's/\(.*\),/\1./' | sort -r | sed 's/*rav://g' | sed "s/:.*//" | rev | sed -n -e '8,16p' | xargs | sed -e 's/ /, /g' | sed '/:[0-9]*$/ ! s/$/./'

I've changed the code to

cat /etc/passwd | sed '/^#/d' | sed -n 'n;p' | sed 's/:\(.*\) //g' | sed 's/ /,/g' | sed 's/\(.*\),/\1./' | sort -r | sed 's/*rav://g' | sed "s/:.*//" | rev | sed -n -e '$FT_LINE1,$FT_LINE2+p' | xargs | sed -e 's/ /, /g' | sed '/:[0-9]*$/ ! s/$/./'

but I got a error

sed: 1: "$FT_LINE1,$FT_LINE2p": invalid command code F

Upvotes: 2

Views: 3391

Answers (2)

codeforester
codeforester

Reputation: 42999

Variables enclosed inside single quotes are not expanded by shell and hence your sed command sees the argument $FT_LINE1,$FT_LINE2p literally. Use double quotes and you will be fine:

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

See also:

Upvotes: 1

Gary_W
Gary_W

Reputation: 10360

Surround your variables with curly braces so the shell knows where the variable name ends:

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

EDIT - D'oh I can't believe I missed the single quotes. They need to be double quotes as others have pointed out so variable substitution will occur.

Upvotes: 1

Related Questions