Judy
Judy

Reputation: 1871

sed + delete function from bash file

we want to add some function as rm function to bashrc , and after some configuration to remove the function rm

so first the following described the approach how to add the function rm to bashrc

echo '
function rm
{
echo "ERROR rm command not allowed on this machine"
return 1
}
' >>/root/.bashrc

and then reload

source /root/.bashrc

in some stage in our scripts we need to do the opposite approach - delete the function rm

function rm
{
echo "ERROR rm command not allowed on this machine"
return 1
}

so I create the following sed

sed -i '1,/function/p;/}/,$p' /root/.bashrc

but I not so feel good with above approach - deletion the function

I will appreciate to get better approach to delete the function rm, from /root/.nashrc

Upvotes: 2

Views: 220

Answers (1)

sseLtaH
sseLtaH

Reputation: 11237

Assuming this is the only function rm line in your bashrc file, you could initially do a dry run

$ sed '/function rm/,/}/d' /root/.bashrc

If happy with it, then add the -i flag.

If the above is true and as you know the amount of lines inserted, you could also use this approach.

$ sed '/function rm/,+4d' /root/.bashrc

Upvotes: 3

Related Questions