Reputation: 75
I want to execute this comand in bash scripting.
Code:
#!/bin/bash
line=39;
d=d; ## For delete line
`echo "sed '$line$d' /etc/passwd"`;
But when I execute, I got this error:
sed: -e expresion #1, character 1: unknow command <<'>>
I tried with echo "sed \'$line$d\' /etc/passwd"
;
But same problem...
Upvotes: 0
Views: 16127
Reputation: 10039
#!/bin/bash
# delete line 39 by editing inline via sed;
sed -i '39d' /etc/passwd
Keep it simple and add the comment if you want remember the sed action
Upvotes: 0
Reputation: 4551
To delete line 39 simply do:
#!/bin/bash
line=39;
sed -i "${line}d" /etc/passwd
Sed will take the second argument as input file and the first as a command. The extra flag -i
will allow you to redirect the output immediately to the input file. Note that this is not the same as sed 'command' file > file
as this will result in an empty file.
I also advise executing man sed
in your shell.
Upvotes: 1
Reputation: 84561
You need to insure when calling your variables that you do not prevent expansion by single-quoting
them. Now it is OK to use single-quotes
within double-quotes
, but be mindful. Also, though not required, when putting two variables back-to-back, it is better to use braces to insure against ambiguity. That said, your sed
command from a script works fine as:
sed -e "${line}${d}" /etc/passwd
Which will output /etc/passwd
to stdout
minus line $line
. To edit /etc/passwd
in place, use sed -i
Upvotes: 2