I'm writing a shell script that I want to add an alias
to the end of the alias
list in the .bashrc
file. I'm thinking something with sed
would work, just not sure how to find the last line that begins with alias
, then add on the next line another alias
.
Why do you need to add it to an "alias list"? If you don't have additional requirements that you did not specify in the question, just append your alias to .bashrc
:
echo "alias youralias='yourcmd'" >> /home/user/.bashrc
When I hear "do something after the last whatever", I think of reversing the file and do something when I see the first whatever:
tac .bashrc |
awk -v newalias="alias new=foo" '$1 == "alias" {print newalias} 1' |
tac > .bashrc.new
mv .bashrc .bashrc.$(date +%Y%m%d%H%M%S) && mv .bashrc.new .bashrc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With