Home > Blockchain >  sed delete function from bash file
sed delete function from bash file

Time:04-06

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

CodePudding user response:

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
  • Related