-1

I'm trying to create a bash script to change the titles of my terminal windows so I can identify what they are doing. I spent a few hours on this and cant figure it out. The idea is to be able to execute settitle NewTitle. Thank you.

This is my echo:$PATH. It looks like Users/klik/bin is there twice. Maybe that is the issue?

~ klik echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/klik/bin:/Users/klik/bin

This is the script which was created in textedit in plain text format.

#!/bin/bash

# settitle: set the Mac Terminal title
# usage:    to set the titlebar to 'PLAY', type:  settitle PLAY

echo -e "\033]0;${1}\007\c"

This is my bash_profile and bin file.

if [ -f ~/.bashrc ]; then
        source ~/.bashrc
fi
export PATH=$PATH:$HOME/bin
alias desk='cd ~/Desktop/'
alias down='cd ~/Downloads/'
alias github='cd ~/github/'

bash_profile

This is my ls -a output ls -a output

Current directories Current Directories

~ klik ls -l $HOME/bin | pbcopy

total 8 -rwx--x--x@ 1 klik staff 147 Mar 9 21:39 settitle.sh

armand
  • 693
  • 9
  • 29
  • did you try `./settitle` ?? Your file is not in `/bin/`, but in '/Users/klik/bin` so you would need to do '/Users/klik/bin/settitle` for a full path. – chicks Mar 09 '16 at 20:31
  • i tried ./settitle but didnt work. I tried to put it /bin and it would not take which was ok because it seemed like a better idea to keep that folder as i got. I followed other instructions to make the script specific to my user which is why its in /Users/klik/bin/settitle. – armand Mar 09 '16 at 20:37
  • 1
    Please post text *as text*, not as images. – Keith Thompson Mar 09 '16 at 23:09

2 Answers2

0

Try this:

echo -e "\033]0;FreddyFrog\007\c"

You need to use -e to turn on interpretation of escape characters. You can also use printf.

printf '\033]0;%s\007\015' "Hippo Croco Horror Pig"
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • made the edit. didnt work. thisis the command and output ~ klik settitle TEST -bash: settitle: command not found – armand Mar 09 '16 at 19:28
  • How did you run it? Did you make the script executable? If not, you need to type `chmod +x settitle` in the Terminal. – Mark Setchell Mar 09 '16 at 19:30
  • See the 2nd image bottom half. I am getting that the directory doesnt exist though it clearly does. Know what to make of that? – armand Mar 09 '16 at 19:54
  • You need to make your mind up what you are doing. Is the file called `settitle` or is it called `settitle.sh`? And whatever it is called, it is not in your HOME directory. The best solution, IMHO, is to make a directory called `bin` in your HOME directory, and put all your scripts in there. Then edit your `.profile` to add `$HOME` to your PATH, i.e. `export PATH=$PATH:$HOME/bin`. Then in $HOME/bin, run `chmod +x *` to make all scripts in $HOME/bin executable. – Mark Setchell Mar 09 '16 at 20:02
  • i added a pic of my bin/bash and home directory. the settitle.sh file is in the bin directory in the home directory(there is an ls in bin folder showing it above. I changed the export path in .bash_profile to export PATH=$PATH:$HOME/bin and still no joy. I believe .profile and .bash_profile are the same for this purpose according to this http://stackoverflow.com/questions/6751252/difference-between-profile-and-bash-profile-on-snow-leopard. Still now working. path to script is /Users/klik/bin/settitle.sh – armand Mar 09 '16 at 20:27
  • Mmmm... please start Terminal and type `echo $PATH | pbcopy` to copy your PATH to the clipboard. The click `edit` under your question and go to the end of your question and press `Command-V` to paste your PATH so I can see it. – Mark Setchell Mar 09 '16 at 20:49
  • Then, in Terminal, type `ls -l $HOME/bin | pbcopy` and click `edit` under your original question and go to the end and press Command-V to paste the output of `ls`. – Mark Setchell Mar 09 '16 at 20:50
  • ~klik echo $PATH | pbcopy /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/klik/bin:/Users/klik/bin ~ klik ls -l $HOME/bin total 8 -rwx--x--x@ 1 klik staff 147 Mar 9 21:39 settitle.sh – armand Mar 09 '16 at 20:54
  • And if you now run `settitle.sh FRED`? – Mark Setchell Mar 09 '16 at 21:11
  • ~ klik settitle.sh FRED /Users/klik/bin/settitle.sh: line 6: unexpected EOF while looking for matching `"' /Users/klik/bin/settitle.sh: line 7: syntax error: unexpected end of file ~ klik settitle FRED -bash: settitle: command not found – armand Mar 09 '16 at 21:48
  • You haven't got line 6 right in the file `settitle.sh` – Mark Setchell Mar 09 '16 at 22:28
  • Are you editing the file with `TextEdit`? If so, you need to click on `Format` at the very top left of the screen and then `Make Plain Text`. Bash doesn't understand word processor files in RTF format with fancy quotes and italics and bold and things - it is a proper Unix tool that wants things straight and simple! :-) – Mark Setchell Mar 09 '16 at 22:32
  • Dont know why that was in there but i fixed it. I had to cut and paste the straight one. still not working: ~ klik settitle FRED -bash: settitle: command not found ~ klik settitle.sh FRED -bash: settitle.sh: command not found ~ klik – armand Mar 09 '16 at 22:50
-1

This issue above was that the file was saved with .txt extension. I dont know why this was the case given the ls command showed a .sh ext. At any rate, this is the process I used for creating this script and and executing it.

Open Finder -> Applications->TextEdit in Mac. Select New Document at bottom left. From menu select Format -> Make Plain Text Paste in this code:

#!/bin/sh

# settitle: set the Mac Terminal title
# usage:    to set the titlebar to 'PLAY', type:  settitle PLAY

echo "\033]0;${1}\007\c"

Thanks to Alvin Alexander for the code.

Still in TextEdit select menu File -> Save Uncheck "If no extension is provided, use ".txt" " When I chose my file name I saved it with no extension so i could just type the command settitle NewTitle without having to type the extension every time. Note the folder the file is being saved to. It defaults to desktop on my machine.

Open Finder -> Go -> Go to Folder

Type in the path to your User Bin folder: mine was /Users/klik/bin You can check to see if you have a User/bin folder by running: ls -l from your home directory. If you don't have a bin folder in this directory you can create one by going to your $HOME directory and executing:

mkdir bin

To find out what is your home directory see this

You can then open the directory by executing:

open bin

This will open the folder in Finder. Drag the script file you created into this folder. Make sure the script is executable by executing the following command from the folder the file is in or by including the path to the file in name of file:

chmod +x <name of file>

Make sure that the script is in your executable $PATH by executing:

echo $PATH

You will get something like this:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/<you>/bin

If you dont see the path of your script, ie /Users/''/bin, then the script file is not in your executable path and you need to put it in your .bash_profile. Execute ls -l to see if you have a .bash_profile file.

ls -l

If you don't have one, make sure your are in your $HOME directory then create one by executing:

mkdir .bash_profile

Open your .bash_profile file in your default editor:

open .bash_profile

Or open with nano (to save and close nano see this link):

nano .bash_profile

Add the following line to the .bash_profile then save/close:

export PATH=$PATH:$HOME/bin

Exit the terminal to reset by executing:

exit

Open the terminal then type:

settitle <whateveryouwant>

I hope this saves someone some time. Thanks to Mark Setchell for his constructive help.

Community
  • 1
  • 1
armand
  • 693
  • 9
  • 29