MacOS – .bash_profile not updating until open new Terminal window

bashemailmacosterminal

I am trying to create a Terminal command. I have created this so far:

cd ~/ && touch .bash_profile && echo "" >> .bash_profile && echo "alias sendtext=\"osascript -e 'on run argv' -e 'tell application \\\"Messages\\\"' -e 'set myid to get id of first service' -e 'set address to item 1 of argv' -e 'set message to item 2 of argv' -e 'set receiver to buddy address of service id myid' -e 'send message to receiver' -e 'end tell' -e 'end run'\"" >> .bash_profile && echo "alias sendtext-remove=\"cd ~/ && grep -vwE \\\"(sendtext|sendtext-remove)\\\" .bash_profile > .bash_profile && . .bash_profile\"" >> .bash_profile && . .bash_profile

It creates two commands:

  • sendtext [email] [message] (Sends a text message to email)
  • sendtext-remove (Removes the two lines from the .bash_profile)

The problem is that after I run sendtext-remove, I can still run send text. How can I update the .bash_profile without opening a new Terminal window?

Best Answer

That seems like an awful lot of work for

alias sendtext-remove='unalias sendtext sendtext-remove'

-- are you trying to cover your tracks in some way by editing the file?

Also note that grep -v foo myfile > myfile will truncate "myfile" to zero bytes! That's because the redirection happens first, and then grep has an empty file to work with. Then . ~/.bashrc is sourcing an empty file and will make no changes to your currently running shell.

Basically, your issue is that you don't unalias the aliases in your current shell.


I was going to write up some functions for install and uninstall, but I've changed my mind. I don't think you should be editing your users' dotfiles for them. If they want your sendtext function you can share it with them, and if they don't want it then they can remove it themselves. I would write it as a function though, just for readability

sendtext() {
    osascript -e 'on run argv' \
              -e 'tell application "Messages"' \
              -e 'set myid to get id of first service' \
              -e 'set address to item 1 of argv' \
              -e 'set message to item 2 of argv' \
              -e 'set receiver to buddy address of service id myid' \
              -e 'send message to receiver' \
              -e 'end tell' \
              -e 'end run' \
              "$@"
}