2

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?

8
  • That seems like a useful alias. Why are you trying to remove it? Mar 4, 2016 at 22:31
  • @glennjackman I am trying to make it uninstallable. Mar 4, 2016 at 22:35
  • In that case, I'd put your aliases (I'd use functions) into a separate file, and edit the bash profile to read it. Also, if uninstalling your code wiped out my .bash_profile, you would have a very angry customer -- take better care when removing that code (use sed instead) Mar 4, 2016 at 22:39
  • @glennjackman Can you give me an example of how I should use sed? (I want to remove the lines that contain my script) Mar 4, 2016 at 22:50
  • I'll expand my answer when I'm not in transit Mar 4, 2016 at 22:53

2 Answers 2

2

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' \
              "$@"
} 
3
  • Would this work instead of grep? echo $(sed '/sendtext/d' .bash_profile) > .bash_profile Mar 4, 2016 at 23:12
  • Nope. Same problem as before: the redirection destroys the file before echo / sed start up. Also this form echo $(some_command) will remove any and all newlines from the output of some_command. Mar 5, 2016 at 0:43
  • sed: check your sed man page -- an example: sed -i ".bak" '/sendtext/d' somefile will edit a file in-place. Mar 5, 2016 at 0:59
2

You can reload the bash profile from command line or script with the following command:

source ~/.bash_profile
1
  • 1
    I am basically doing that with . .bash_profile (I cd into ~/ already) Mar 4, 2016 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .