0

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...

0

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think d should be expanded, he wants to remove using sed I believe. Right now there isn't even a sed command... The problem, it seems to me, was that he was trying to end the variable with a dollar instead of encapsulating it with curly braces.
Look at the code in pastebin immediately before the call it does d=d
Yes I overlooked that part, my bad. This is definitely the better answer :)
Your solution was perfect David C. Rankin! Works fine.
1

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.

4 Comments

delete line 39 from where? output to stdout? or in /etc/passwd?
/etc/passwdis the input file, check his code in the pastebin.
I did, sed foo /etc/password only operates on cat /etc/passwd. You must include -i for edit in place.
You're right, I was being sloppy! Consider my answer revoked!! Thanks for pointing that out.
0
#!/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

2 Comments

The problem is that I dont know the line, I get line number counting in a for, and comparing names.
in this case, the @ShellFish reply is fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.