linux bash 变量_Linux Bash环境变量

linux bash 变量

linux bash 变量

Environment is shell running session and session related info. environment is important part of the Linux bash. We can set variables at the current environment and use them. Bash also provides some default environment variables too.

环境是外壳程序运行的会话以及与会话相关的信息。 环境是Linux bash的重要组成部分。 我们可以在当前环境中设置变量并使用它们。 Bash还提供了一些默认的环境变量。

登录时创建变量 (Create Variables While Login)

You may need to create variable at login time of the user. For example to use login date at shell scripts it is very convenient or looking data from other sources and setting for session is an other reason to create variables while login.There are different locations to make this possible.

您可能需要在用户登录时创建变量。 例如,在shell脚本中使用登录日期非常方便,或者从其他来源查找数据,并且设置会话设置是在登录时创建变量的另一个原因。在不同的位置可以做到这一点。

  • /etc/profile is the start point for shell creation. There is all ready a lot of script in this file. You can add simple at the end of file. This file needs user login to execute.

    / etc / profile是创建shell的起点。 这个文件中已经准备好了很多脚本。 您可以在文件末尾添加简单。 该文件需要用户登录才能执行。

  • /etc/profile.d/ is similar with profile file but serves more autonomous place to make changes. profile.d is directory that contains script files those are executed at run time. If you will need complex and more than few line script you can create a script file here. This file needs user login to execute

    /etc/profile.d/与配置文件相似,但提供了更多的自主位置进行更改。 profile.d是包含在运行时执行的脚本文件的目录。 如果您需要复杂的行脚本,可以在这里创建一个脚本文件。 该文件需要用户登录才能执行

  • ~/.bashrc is different from other because of the code executed without a login which means if there is no user that login we should use this file.To make complex script create another file and run it from bash.rc

    〜/ .bashrc与其他代码不同,因为没有登录就执行了代码,这意味着如果没有用户登录我们应该使用此文件。要创建复杂的脚本,请创建另一个文件并从bash.rc运行

环境变量(Environment Variables)

Variables for bash can be set for different places. There are 4 types of variables

bash的变量可以在不同的地方设置。 变量有4种

  • String

  • Integer

    整数
  • Constant

    不变
  • Array

    数组

出口(Exporting)

Like normal variables environment variables are case sensitive too. Exporting variables are declared with export keyword. Export keyword provides that variable will exist other shells too.

像普通变量一样,环境变量也区分大小写。 导出变量使用export关键字声明。 Export关键字提供了变量也将存在其他外壳程序。

$ export test="This is test" 
$ env | grep test
test=This is test

If you open new terminal you can see the defined variable but if you close existing terminal and look test variable in other terminal you can not find it because environment variables are only used by child terminals. Child terminal is a terminal that is created by existing terminal.

如果打开新终端,则可以看到定义的变量,但是如果关闭现有终端并在其他终端中查看测试变量,则找不到该变量,因为环境变量仅由子终端使用。 子终端是由现有终端创建的终端。

LEARN MORE  How To Download, Compile and Install Custom Linux Kernel Manually In Ubuntu, Debian, Mint, Kali, CentOS?
了解更多如何在Ubuntu,Debian,Mint,Kali,CentOS中手动下载,编译和安装自定义Linux内核?

To unset environment variable use -n parameter with export like this. This will make environment variable normal variable.

要取消设置环境变量,可以将-n参数与export一起使用。 这将使环境变量成为普通变量。

env |grep test 
test=5 
export -n test 
env |grep test

创建外壳变量 (Create Shell Variables)

There is default bash variables that helps about current environment. For example to find current bash binary path BASH variable is used.

有默认的bash变量可帮助您了解当前环境。 例如,要查找当前的bash二进制路径,将使用BASH变量。

$ echo $BASH
/bin/bash

查找当前执行的bash命令 (To find currently executed bash command)

$ echo $BASH_COMMAND 
echo $BASH_COMMAND

获取Bash版本 (Get Bash Version)

Sometimes you will get a script from web or there is a problem with an existing script in the new host. There may be some compatibility issues. To get detailed info about compatibility bash version is important. And here is how to get it.

有时您会从Web获得脚本,或者新主机中的现有脚本有问题。 可能存在一些兼容性问题。 获得有关兼容性bash版本的详细信息很重要。 这是如何获得它。

$ echo $BASH_VERSINFO 
4

Find enviroment variables which are set when bash starts executing.

查找bash开始执行时设置的环境变量。

$ env
XDG_VTNR=1
KDE_MULTIHEAD=false
SSH_AGENT_PID=1399
PAM_KWALLET5_LOGIN=/tmp/kwallet5_ismail.socket
XDG_SESSION_ID=2
HOSTNAME=lenovo.baydan
GUESTFISH_INIT=\e[1;34m
SHELL=/bin/bash
...

获取用户运行脚本 (Getting The User Running Script)

While running bash interactively or non-interactively getting current user name is done with $USER special variable. This will be helpful if the script needs root privileges and checking the current user.

以交互方式或非交互方式运行bash时,使用$ USER特殊变量可获取当前用户名。 如果脚本需要root特权并检查当前用户,这将很有帮助。

$ echo $USER 
ismail

获取主机名 (Getting Host name)

Host name is the name of the computer that is currently used. In enterprise environment there are a lot of servers and hosts. It can be distinguishing host name is important for scriptwriters. To get host name where the script is running

主机名是当前使用的计算机的名称。 在企业环境中,有许多服务器和主机。 可以区分主机名对于脚本编写者很重要。 获取脚本运行所在的主机名

$ echo $HOSTNAME 
lab_voip

Shell启动后获得秒数 (Getting Number of Seconds After Shell Started)

To get time between now and shell start time use this special variable. If the shell you use is all ready open for a long time you will get a big number of course.

要获取从现在到外壳启动时间之间的时间,请使用此特殊变量。 如果您所使用的外壳已经准备好长时间开放,您将获得大量的机会。

$ echo $SECONDS 
2770

产生随机数 (Generating Random Numbers)

To generate random numbers use the following command. Be aware that this random number generation method may not be photographically secure. To generate most secure random numbers use Openssl library with extra hardware. But Openssl random number generation is enough for a lot of situation.

要生成随机数,请使用以下命令。 请注意,这种随机数生成方法可能并不安全。 要生成最安全的随机数,请使用带有额外硬件的Openssl库。 但是Openssl随机数生成足以应付许多情况。

$ echo $RANDOM 
8500

获取Bash脚本的当前行号 (Getting Current Line Number of The Bash Script)

Line number can be used in bash scripts or interactive shell and for both of them gives line number from scratch.

行号可以在bash脚本或交互式shell中使用,并且两者都从头给出行号。

$ echo $LINENO
67

翻译自: https://www.poftut.com/linux-bash-environment-variables/

linux bash 变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值