官术网_书友最值得收藏!

  • Ubuntu 20.04 Essentials
  • Neil Smyth
  • 377字
  • 2021-06-11 17:39:43

9.12 Environment Variables

Shell environment variables provide temporary storage of data and configuration settings. The shell itself sets up a number of environment variables that may be changed by the user to modify the behavior of the shell. A listing of currently defined variables may be obtained using the env command:

$ env

SSH_CONNECTION=192.168.0.19 61231 192.168.0.28 22

MODULES_RUN_QUARANTINE=LD_LIBRARY_PATH

LANG=en_US.UTF-8

HISTCONTROL=ignoredups

HOSTNAME=demo-pc.ebookfrenzy.com

XDG_SESSION_ID=15

MODULES_CMD=/usr/share/Modules/libexec/modulecmd.tcl

USER=demo

ENV=/usr/share/Modules/init/profile.sh

SELINUX_ROLE_REQUESTED=

PWD=/home/demo

HOME=/home/demo

SSH_CLIENT=192.168.0.19 61231 22

SELINUX_LEVEL_REQUESTED=

.

.

.

Perhaps the most useful environment variable is PATH. This defines the directories in which the shell will search for commands entered at the command prompt, and the order in which it will do so. The PATH environment variable for a user account on a newly installed Ubuntu system will likely be configured as follows:

$ echo $PATH

/home/demo/.local/bin:/home/demo/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Another useful variable is HOME which specifies the home directory of the current user. If, for example, you wanted the shell to also look for commands in the scripts directory located in your home directory, you would modify the PATH variable as follows:

$ export PATH=$PATH:$HOME/scripts

The current value of an existing environment variable may be displayed using the echo command:

$ echo $PATH

You can create your own environment variables using the export command. For example:

$ export DATAPATH=/data/files

A useful trick to assign the output from a command to an environment variable involves the use of back quotes (`) around the command. For example, to assign the current date and time to an environment variable called NOW:

$ export NOW=`date`

$ echo $NOW

Tue Apr 2 13:48:40 EDT 2020

If there are environment variable or alias settings that you need to be configured each time you enter the shell environment, they may be added to a file in your home directory named .bashrc. For example, the following .bashrc file is configured to set up the DATAPATH environment variable and an alias:

# .bashrc

  

# Source global definitions

if [ -f /etc/bashrc ]; then

        . /etc/bashrc

fi

 

# User specific environment

PATH="$HOME/.local/bin:$HOME/bin:$PATH"

export PATH

 

# Uncomment the following line if you don't like systemctl's auto-paging feature:

# export SYSTEMD_PAGER=

 

# User specific aliases and functions

export DATAPATH=/data/files

alias l="ls -lt"

主站蜘蛛池模板: 临泽县| 蕉岭县| 乌兰浩特市| 平凉市| 东山县| 绍兴县| 绥宁县| 正定县| 渭南市| 吉首市| 响水县| 连平县| 德保县| 宝山区| 枣阳市| 睢宁县| 栖霞市| 万全县| 福海县| 清新县| 舒城县| 吉安市| 永宁县| 都安| 安远县| 万宁市| 蓬莱市| 双峰县| 蓝山县| 武胜县| 鲁甸县| 鄯善县| 同仁县| 苍南县| 锡林浩特市| 嘉黎县| 德昌县| 咸丰县| 云和县| 平顶山市| 九寨沟县|