While working with Bash environment we generally need some variable data to set and get. Bash shell provides some default values about current user, hostname etc. as environment variable. In this tutorial we will look most popular of them.
在Bash环境中工作时,我们通常需要一些可变数据来设置和获取。 Bash shell提供了一些有关当前用户,主机名等的默认值作为环境变量。 在本教程中,我们将最受欢迎。
获取用户运行脚本 (Get The User Running Script)
While running bash interactively or non-interactively getting current user name is done with $USER special variable.
以交互方式或非交互方式运行bash时,使用$ USER特殊变量可获取当前用户名。
$ echo $USER
ismail
获取主机名 (Get Hostname)
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启动后获取秒数 (Get 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
产生随机数 (Generate 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脚本的当前行号 (Get 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
获取当前正在运行的脚本路径 (Get Currently Running Script Path)
Some scripts run a lot of complex operations. through the operations the path of the currently running script is important. To get running bash script current working directory use BASH_SOURCE variable like below.
一些脚本运行许多复杂的操作。 通过这些操作,当前正在运行的脚本的路径很重要。 要获得正在运行的bash脚本当前工作目录,请使用如下所示的BASH_SOURCE变量。
$ echo $BASH_SOURCE
翻译自: https://www.poftut.com/linux-bash-default-environment-variables/