从shell脚本设置环境变量

时间:2016-06-01 09:23:48

标签: bash shell unix

我有一个像

这样的环境变量
ABC_XYZ=false

当我echo $ABC_XYZ时 它给了我false 现在我在脚本中执行这些步骤

>echo $ABC_XYZ 
false
>ABC_XYZ=true
>echo $ABC_XYZ
true

现在退出脚本后 echo $ABC_XYZ再次给了我false 我可以通过任何方式从脚本内部将值设置为true。

提前致谢。

2 个答案:

答案 0 :(得分:2)

您必须使用source在shell的当前上下文中运行脚本。

假设您的脚本位于当前目录中:

$ source ./script_filename

$ . ./script_filename

引自help source

  

source:source filename [arguments]       从当前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.

要使变量在随后的分叉子进程(shell或任何其他进程)中可用,您需要在脚本中导出这样的环境变量:

export ABC_XYZ

答案 1 :(得分:1)

尝试使用以下命令运行以下脚本文件:

 function my_FeedExcluder($query)
 {
       $custom_meta_query = array(
                            array(
                            'key'     =>'meta_key_name',             
                            'value'   => array('value1' , 'Value2'),
                            'compare' => 'IN',
                          ),
                      );

      $query->set('post_type','post');
      $query->set('posts_per_page',10);
      $query->set('orderby','post_date');
      $query->set('meta_key','meta_key_name');
      $query->set('order','DESC');
      $query->set('meta_query',$custom_meta_query);
      return $query;
}

add_filter('pre_get_posts','my_FeedExcluder');
相关问题