相当于 LINUX bash 中的 typeset -l 和 typeset -u?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11084675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:55:59  来源:igfitidea点击:

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

linuxbashkshhp-ux

提问by Justin

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

我正在将脚本从 HP-UX 移植到 LINUX。当我尝试获取脚本时,bash 抱怨说

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 -uand typeset -ldo specifically that's different from a general assignment such as foo="bar".

据我所知,排版是用来给变量赋值的,虽然我不太明白什么typeset -utypeset -l具体做什么,这与诸如 foo="bar" 之类的一般赋值不同。

I was wondering if there was some equivalent way to express typeset -uand typeset -lfor LINUX bash since it does not appear to be compatible with bash.

我想知道是否有一些等效的方式来表达typeset -utypeset -l用于 LINUX bash,因为它似乎与 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.

或者,我想知道是否有可能将排版命令识别为 ksh 命令,因为排版似乎来自 ksh。

Thanks.

谢谢。

采纳答案by chepner

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

你从/移植到什么版本的 bash?typeset -l使变量使得对其的任何赋值都将大写转换为小写;typeset -u将低转换为高。我怀疑这些选项是bash在版本 4 左右的某个时候添加的。

回答by Justin

回答by ormaaj

The behavior of typeset -land -uare 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 -uand -lare 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 -l-u在 Bash、ksh93 和 mksh 中的行为基本相同,它们导致字符串在赋值时分别转换为小写或大写。在 ksh 中,它们还充当长整数和浮点数的修饰符,这些不是常见的 shell 功能(Bash 没有这些功能)。通常不鼓励使用-u-l,特别是在大型脚本中,它们可能会让错误溜进来。 大多数情况下,使用 case-modification 参数扩展有更好的替代方案。

typesetunder Bash is a synonym for declare(Bash considers typesetdeprecated - 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).

typeset在 Bash 下是同义词declare(Bash 认为typeset已弃用 - IMO 这不是主要问题)。它们之间存在许多显着差异,除非您注意了解它们的确切行为,否则它们通常应被视为不兼容。在这两种 shell 中,它们在定义数据类型(Bash、zsh 和 mksh 都具有一些比 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).

此外,在 Linux 下安装 ksh93(或整个 AST 工具包)没有问题,除非您真的想要,否则可能不需要将脚本移植到 Bash。Bash 在 Linux 下作为默认设置更受欢迎,主要是由于历史原因,在一定程度上,许可(copyleft)。