linux - 取消设置 C-Shell 中的所有环境变量

标签 linux shell csh

我想通过 C-shell 脚本取消设置所有环境变量。 使用 unsetenv * 命令,我可以使用命令行来完成此操作。 我们怎样才能让它在我们的脚本中工作呢? 当我运行脚本时,我遇到了未定义的标识符问题

最佳答案

来自tcsh(1):

   unsetenv pattern
           Removes  all  environment  variables whose names match pattern.
           `unsetenv *' thus removes all environment variables; this is  a
           bad idea.  It is not an error for nothing to be unsetenved.

因此使用unsetenv *应该可以工作。您的(可能是旧的)(t)csh 版本可能不支持此功能?

无论哪种方式,正如手册页所说,取消设置一切可能是一个坏主意。您将需要保留各种变量,例如 HOMEUSER 和其他一些变量。更好的方法可能是这样的:

foreach e (`env`)
    set name = `echo "$e" | cut -d "=" -f 1`

    # Note: this list should be longer.
    if ( "$name" != "HOME" ) then
        unsetenv "$name"
    endif
end

关于linux - 取消设置 C-Shell 中的所有环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52922129/

相关文章:

linux - 为什么 bash 脚本中需要 $(( ... )) ?

linux - 如何在特定创建的文件上用 `until.. sleep` 替换 `inotifywait`?

python - shell 脚本不能与 nohup 一起工作

python - 如何隐藏/抑制 fabric ssh 命令输出到控制台?

linux - .NET Core 应用程序可以在仅安装 .NET Core 的服务器上运行吗?

linux - jboss服务器未连接

bash - 如果文件存在始终为 false

bash - 忽略注释行替换文件中字符的快速方法

linux - 如何在csh中定义 'alias'

c - $status 在 C shell 中指的是什么?