noob shell scripting question

Joined
Mar 20, 2009
Messages
2
Reaction score
0
Points
1
Hi all,

Just got this book on shell scripting and thought I'd give it a go.

Here's my script:

Code:
#! /bin/sh

# titleterm - tells Terminal to change its title to the specified value

if [ $# != 1 ]; then
  echo "Usage: $0 title" >&2
  exit 1
else
  echo -n "\033]0;$1\007"
fi

exit 0

My problem is with the "-n" option in the second echo command... if I run echo in the command prompt the "-n" option works as advertised, but in the script, -n is interpreted as the string "-n" and not as an option. I'm guessing it's a syntax issue... any suggestions?

-Mike
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
From the man page for echo(1):

"Some shells may provide a built-in echo command which is similar or identical to this utility. Most notably, the built-in echo in sh(1) does NOT accept the -n option. Consult the builtin(1) manual page."

You can get the -n option to work if you change to using /bin/bash as the shell rather than /bin/sh (in the first line of your script), or else if you invoke /bin/echo via its explicit path to avoid using the built-in version while continuing to use the standard Bourne shell.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top