AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1024512
Accepted
John
John
Asked: 2020-07-09 06:48:33 +0800 CST2020-07-09 06:48:33 +0800 CST 2020-07-09 06:48:33 +0800 CST

如何从 bash 脚本中设置 tmp 环境变量

  • 772

我有一个简单的 bash 脚本:

#!/bin/bash

export MONGOMS_DOWNLOAD_URL="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz"
export MONGOMS_VERSION="4.2.8"

但是当我运行这个'./preinstall.sh'然后echo $MONGOMS_VERSIONvar没有设置。

如果我直接在终端中导出这些变量,则没有问题。

https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell#answer-496777 根据这篇文章,shell脚本具有只读访问权限父级和任何 vars 集都将丢失。

有没有解决的办法?

bash environment-variables
  • 2 2 个回答
  • 262 Views

2 个回答

  • Voted
  1. Best Answer
    Kate
    2020-07-09T08:24:48+08:002020-07-09T08:24:48+08:00

    利用:

    source ./preinstall.sh
    

    或为了更好的便携性:

    . preinstall.sh
    

    来源是点/句点“。”的同义词。在 bash 中,但不是在 POSIX sh 中,因此为了获得最大的兼容性,请使用句点。

    . (源或点运算符)

    从当前 shell 上下文中的文件名参数读取并执行命令。

    见:。(源或点运算符)

    • 1
  2. fpmurphy
    2020-07-09T08:25:51+08:002020-07-09T08:25:51+08:00

    您需要来源./preinstall.sh. 有两种方法可以做到这一点:

    source ./preinstall.sh
    

    或者

    . ./preinstall.sh
    

    Bash 一样,ksh并且zsh 支持.和source。它在当前 shell 中读取并执行指定文件的内容,而不是在新进程中。

    使用bash外壳:

    $ type source
    source is a shell builtin
    $ type .
    . is a shell builtin
    $ source --help
    source: source filename [arguments]
        Execute commands from a file in the current shell.
        
        Read and execute commands from FILENAME in the current shell.  The
        entries in $PATH are used to find the directory containing FILENAME.
        If any ARGUMENTS are supplied, they become the positional parameters
        when FILENAME is executed.
        
        Exit Status:
        Returns the status of the last command executed in FILENAME; fails if
        FILENAME cannot be read.
    $
    

    POSIX 指定了 dot( .) 特殊内置函数,但对 . 保持沉默source。从标准:

    NAME
    dot - execute commands in the current environment
    SYNOPSIS
    . file
    
    DESCRIPTION
    The shell shall execute commands from the file in the current environment.
    
    If file does not contain a <slash>, the shell shall use the search path specified by PATH to find the directory containing file. Unlike normal command search, however, the file searched for by the dot utility need not be executable. If no readable file is found, a non-interactive shell shall abort; an interactive shell shall write a diagnostic message to standard error, but this condition shall not be considered a syntax error.
    
    OPTIONS
    None.
    

    为了获得最大的 shell 脚本可移植性,您应该只使用不带参数的 dot 命令。

    顺便说一句,如果您从位置可能发生变化的脚本中采购,我建议您使用绝对路径而不是相对路径。

    • 1

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve