1

I'm in the process of porting over a script from HP-UX to LINUX. When I try to source the script, bash complains that

bash: typeset: -u: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...

typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
./install_profile: line 237: typeset: -l: invalid option

From what I can see, typeset is used to assign a value to a variable, although I don't quite understand what typeset -u and typeset -l do specifically that's different from a general assignment such as foo="bar".

I was wondering if there was some equivalent way to express typeset -u and typeset -l for LINUX bash since it does not appear to be compatible with bash.

Altneratively, I was wondering if it would be possible to get the typeset commands recognized as ksh commands, since it appears that typeset is from ksh.

Thanks.

Gabe
  • 84,912
  • 12
  • 139
  • 238
Justin
  • 742
  • 5
  • 17
  • 34
  • What version of `bash` are you using, since I cannot reproduce the issue. `typeset -u` and `typeset -l` are working fine here on Debian with bash version 4.1.5 – dwalter Jun 18 '12 at 14:18

3 Answers3

2

What versions of bash are you porting from/to? typeset -l makes the variable such that any assignment to it converts upper case to lower case; typeset -u converts lower to upper. I suspect those options were added to bash sometime around version 4.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • I'm using an older version of bash (namely GNU bash, version 3.2.39(1)-release (i586-suse-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc.) – Justin Jun 18 '12 at 14:20
  • There you go. If you can't upgrade to a newer version of bash, just drop the `-l` and `-u` from the calls to typeset. However, keep in mind the rest of the script will assume that the relevant variables are all upper-case (or lower-case, accordingly). Either find another way to do the conversion, or adapt the script to check for case explicitly. – chepner Jun 18 '12 at 14:23
  • Thanks, your answer helped me find a relevant answer! – Justin Jun 18 '12 at 14:29
2

The behavior of typeset -l and -u are basically the same in Bash, ksh93, and mksh, where it they cause strings to be converted to lower or uppercase respectively on assignment. In ksh, they additionally act as modifiers for long ints and floats, which aren't common shell features (Bash doesn't have these). Using -u and -l are generally discouraged especially in large scripts where they can let bugs slip in. There are better alternatives most of the time using the case-modification parameter expansions.

typeset under Bash is a synonym for declare (Bash considers typeset deprecated - IMO this isn't a major issue). There are many significant differences between them and they should generally be considered incompatible unless you take care to know their exact behavior. In both shells, they play a major role in defining datatypes (Bash, zsh, and mksh all have some non-overlapping support that's much more limited than ksh93).

Also, there's no problem with installing ksh93 (or the whole AST toolkit) under Linux and probably no need to port your script to Bash unless you really want to. Bash is far more popular as a default under Linux mainly for historical reasons, and to a certain extent, licensing (copyleft).

ormaaj
  • 6,201
  • 29
  • 34
1
$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'

Source: http://www.cyberciti.biz/faq/linux-unix-shell-programming-converting-lowercase-uppercase/

Justin
  • 742
  • 5
  • 17
  • 34