I wrote this function which I use to add aliases to .bashrc file. The function works well but it's not complete, I would like to ask for confirmation from the user if the alias being added already exists and write the if condition in order to achieve the "modified" part of the code if confirmation is given, just like when you install a new package.
add_alias(){
d_alias=$1
d_command="$2"
replacing=alias|grep "alias $d_alias"
if [[ "$replacing" -ne 0 ]];
then
sed -i "/alias $d_alias/d" $HOME/.bashrc
echo "alias $d_alias modified in ~/.bashrc"
else
sed -i ':a;$!{N;ba};s,\(auto-generated code\),\1\nalias '"$d_alias"'='"'$d_command'"',2' $HOME/.bashrc
source ~/.bashrc
echo "alias $d_alias added to ~/.bashrc"
fi
}
#auto-generated code
alias brc='source ~/.bashrc'
alias client='/home/user/workspace/client'
alias workspace='/home/user/workspace'
I'm adding an answer for future reference, credits to @4ae1e1 and @ArunSangal. Their suggestions are in the comments.