Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding alias to end of alias list in .bashrc file using sed

Tags:

bash

shell

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.

like image 304
Danny Avatar asked Nov 28 '22 18:11

Danny


2 Answers

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
like image 68
Andrea Spadaccini Avatar answered Dec 06 '22 07:12

Andrea Spadaccini


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
like image 38
glenn jackman Avatar answered Dec 06 '22 09:12

glenn jackman