Equivalent for typeset -l and typeset -u in LINUX bash?

11,252

Solution 1

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.

Solution 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).

Share:
11,252
Justin
Author by

Justin

Hi. My name is Justin. I recently graduated from the University of Waterloo in Waterloo, ON, Canada with a Bachelor of Applied Science in Computer Engineering. I aspire to continue developing my skills in software and embedded systems development.

Updated on June 04, 2022

Comments

  • Justin
    Justin almost 2 years

    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.

    • dwalter
      dwalter almost 12 years
      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
  • Justin
    Justin almost 12 years
    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.)
  • chepner
    chepner almost 12 years
    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.
  • Justin
    Justin almost 12 years
    Thanks, your answer helped me find a relevant answer!