unset命令有什么作用 linux

unset命令有什么作用 linux,第1张

Linux unset命令

功能说明:unset是一个内建的Unix shell命令,在Bourne shell家族(sh、ksh、bash等)和C shell家族(csh、tcsh等)都有实现。它可以取消设置一个shell变量,从内存和shell的导出环境中删除它。它实现为一个壳层内建指令,因为它直接 *** 纵shell的内部。

只读shell变量不能被unset。如果试图unset只读变量,unset命令将打印一个错误消息,并返回一个非零的退出代码。

This command removes one or more variables. Each name is a variable name, specified in any of the ways acceptable to the set command. If aname refers to an element of an array then that element is removed without affecting the rest of the array. If a name consists of an array name with no parenthesized index, then the entire array is deleted. The unset command returns an empty string as result. An error occurs if any of the variables doesn't exist, and any variables after the non-existent one are not deleted.

语法:

unset [-fv][变量或函数名称]

补充说明:unset为shell内建指令,可删除变量或函数。

参   数:

-f  仅删除函数。

-v  仅删除变量。

[3][root][test:/home/root/Desktop] set lx = "ls -lh"

[3][root][test:/home/root/Desktop] $lx

total 80K

drwxrwxr-x 2 root tt 4.0K 2014-02-19 09:32 bin

drwxrwxr-x 3 root tt 4.0K 2014-02-10 10:56 Data

drwxrwxr-x 2 root tt 4.0K 2014-02-19 09:32 lib

drwxrwxr-x 10 root tt 4.0K 2014-02-20 16:12 script

-r--r--r-- 1 root tt 312 2014-02-19 09:32 sut.cs

[3][root][test:/home/root/Desktop]

[3][root][test:/home/root/Desktop]

[3][root][test:/home/root/Desktop] set

......此处省略输出

[3][root][test:/home/root/Desktop] set |grep -i lx

lx ls -lh

[3][root][test:/home/root/Desktop]

[3][root][test:/home/root/Desktop] unset lx

[3][root][test:/home/root/Desktop] set | grep -i lx

set命令:

Linux and Unix set command

Quick links

About set

Syntax

Examples

Related commands

Linux and Unix main page

About set

set is a built-in function of the Bourne shell (sh), C shell (csh), and Korn shell (ksh), which is used to define and determine the values of the systemenvironment.

Syntax

Bourne shell (sh):

set [--aefhkntuvx[argument]]...

C shell (csh):

set [var[=value]]

set var [n] = word

Korn shell (ksh):

set [+-aefhkmnopstuvx] [+-o option]... [+-A name] [arg...]

Options: Bourne Shell (sh)

In sh, the set built-in command has the following options:

--An option of a double-dash ("--") signifies the end of an option list. This is primarily useful when values listed after the options will start with a dash themselves.

-aMark variables which are modified or created for "export"environment variables set in this way will be passed on to the environments of any subsequent commands.

-eExit immediately if a command exits with a non-zero exit status.

-fDisable file name generation (globbing).

-hLocate and remember function commands as functions are defined (function commands are normally located when the function is executed).

-kAll keyword arguments are placed in the environment for a command, not just those that precede the command name.

-nRead commands but do not execute them.

-tExit after reading and executing one command.

-uTreat unset variables as an error when substituting.

-vPrint shell input lines as they are read.

-xPrint commands and their arguments as they are executed.

Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell itself. The current set of flags may be found in the variable $-. The remaining arguments are positional parameters and are assigned, in order, to $1, $2, .... If no arguments are given the values of all names are printed.

For each name, the unset command removes the corresponding variable or function value. The special variables PATH, PS1, PS2, MAILCHECK, and IF cannot be unset.

With the export built-in command, the given names are marked for automatic export to the environment of subsequently executed commands. If no arguments are given, variable names that have been marked for export during the current shell's execution are listed. Function names are not exported.

Options: C Shell (csh)

In csh, If no arguments are specified, set displays the values of all shell variables. Multiword values are displayed as a parenthesized list. With thevar argument alone, set assigns an empty (null) value to the variable var. With arguments of the form var = value, set assigns value to var, wherevalue is one of:

wordA single word (or quoted string).

(wordlist)A space-separated list of words enclosed in parentheses.

Values are command and filename expanded before being assigned. The form set var[n]=word replaces the n'th word in a multiword value withword.

unset removes variables whose names match (using filename substitution) pattern. All variables are removed by "unset *"this is a very bad idea if you don't know what you're doing, though.

Options: K Shell (ksh)

In ksh, the set command takes the following options:

-AArray assignment. Unset the variable name and assign values sequentially from the list arg. If +A is used, the variable name is not unset first.

-aAll subsequent variables that are defined are automatically exported.

-eIf a command has a non-zero exit status, execute the ERR trap (if set), and exit. This mode is disabled while reading profiles.

-fDisables file name generation (globbing).

-hEach command becomes a tracked alias when first encountered.

-kAll variable assignment arguments are placed in the environment for a command, not just those that precede the command name.

-mBackground jobs will run in a separate process group and a line will print upon completion. The exit status of background jobs is reported in a completion message. On systems with job control, this flag is turned on automatically for interactive shells.

-nRead commands and check them for syntax errors, but do not execute them. Ignored for interactive shells.

-ooptionThe option argument can be one of the following:

allexportSame as -a.

errexitSame as -e.

bgniceAll background jobs are run at a lower priority. This is the default mode.

emacsPuts you in an emacs-style in-line editor for command entry.

emacsPuts you in a gmacs-style in-line editor for command entry.

ignoreeofThe shell will not exit on end-of-file. The command exit must be used.

keywordSame as -k.

markdirsAll directory names resulting from file name generation have a trailing "/" appended.

monitorSame as -m.

noclobberPrevents redirection ">" from truncating existing files. Require ">|" to truncate a file when turned on.

noexecSame as -n.

noglobSame as -f.

nologDo not save function definitions in history file.

nounsetSame as -u.

privilegedSame as -p.

verboseSame as -v.

trackallSame as -h.

viPuts you in insert mode of a vi-style in-line editor until you hit escape. This puts you in control mode. A return sends the line.

virawEach character is processed as it is typed in vi mode.

xtraceSame as -x.

If no option name is supplied then the current option settings are printed.

-pDisables processing of the $HOME/.profile file and uses the file /etc/suid_profile instead of the ENV file. This mode is on whenever the effective uid (user ID) is not equal to the real uid, or when the effective gid (group ID) is not equal to the real gid. Turning this off causes the effective uid and gid to be set to the real uid and gid.

-sSort the positional parameters lexicographically.

-tExit after reading and executing one command.

-uTreat unset parameters as an error when substituting.

-vPrint shell input lines as they are read.

-xPrint commands and their arguments as they are executed.

-Turns off -x and -v flags and stops examining arguments for flags.

-Do not change any of the flagsuseful in setting $1 to a value beginning with -. If no arguments follow this flag then the positional parameters are unset.

Using + rather than - causes these flags to be turned off. These flags can also be used upon invocation of the shell. The current set of flags may be found in $-. Unless -A is specified, the remaining arguments are positional parameters and are assigned, in order, to "$1 $2 ...". If no arguments are given, then the names and values of all variables are printed on the standard output.

The variables given by the list of names are automatically unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM, SECONDS,TMOUT, and _ removes their special meaning even if they are subsequently assigned.

When using unset, the variables given by the list of names are similarly unassigned: their values and attributes are erased. Readonly variables cannot be unset. If the -f, flag is set, then the names refer to function names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND,RANDOM, SECONDS, TMOUT, and _ removes their special meaning even if they are subsequently assigned.

With the export built-in command, the given names are marked for automatic export to the environment of subsequently-executed commands. kshcommands that are preceded by one or two "*" characters (asterisks) are treated specially in the following ways:

Variable assignment lists preceding the command remain in effect when the command completes.

I/O redirections are processed after variable assignments.

Errors cause a script that contains them to abort.

Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed.

Examples

set PATH="/bin:/usr/bin:/usr/sbin:usr/local/bin"

In csh, this command sets the environment variable PATH, such that the shell will search for files in the /bin, /usr/bin, /usr/sbin and /usr/local/bindirectories, in that order.

Related commands

csh

ksh

setenv

sh

背景:

阅读英文原作只为了更好地理解书中的精髓,当然也是为了更好地掌握书中的技巧。我们会坚持阅读全书,并记录书中的精髓。

Many people speak of "freedom" with regard to Linux. Freedom is the power to decide what your computer does and the only way to have this freedom is to know what your computer is doing. Freedom is a computer that is without any secrets, one where everything can be known if you care enough to find it.

很多人一说到Linux, 就会说自由。但是你真的而理解这种自由吗?这种自由是一种权力,能够决定你的计算机干什么,获取这种自由的唯一方式就是知道你的计算机在做什么。自由就是你的计算机没有任何㊙️,你可以从计算机那里了解一切,只要你用心去寻找。

We speak of the command line, we are really referring to the shell. The shell is a program that takes keyboard commands and passes them to the operating system to carry out.

一说起命令行,我们真正指的是 shell。shell 就是一个程序,把键盘的输入传递给 *** 作系统去执行。

第一次见到 shell,你只会看到一行提示符,形如: [me@linuxbox ~]$

This is called a shell prompt and it will appear whenever the shell is ready to accept input.

这叫做 shell 提示符, 无论何时当 shell 准备好接受输入时,就会闲这样的提示符。当然,出现的形式会根据不同的 Linux 发行版有所不同。但是大体上会包含 主机名 用户名 当前目录 和一个 美元符号 。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7321763.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存