13-A.2: env / alias / PATH / time
- Page ID
- 43160
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The env Command
The env command is used to print environment variables. It is also used to run a utility or command in a custom environment. In practice, env has another common use. It is often used by shell scripts to launch the correct interpreter. In this usage, the environment is typically not changed.
Environmental Variable | Meaning | View Variable by Entering: |
---|---|---|
HOSTNAME | Name of this computer. | echo $HOSTNAME |
HISTSIZE | Number of commands kept in the history defaults to 500. | echo $HISTSIZE |
HOME | Home directory of the current user. | echo $HOME |
LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_. | echo $LANG |
PATH | Search path for commands. It is a colon-separated list of directories where the shell looks for commands being entered. | echo $PATH |
PS1 | Current prompt settings. | echo $PS1 |
TERM | Login terminal type. | echo $TERM export TERM=vt100 |
SHELL | Path to login shell. | echo $SHELL |
EDITOR | Name of default text editor for this user. | export EDITOR=/usr/bin/vim |
You can view all environment variables set on your system with the env command. The list is long, so pipe the output through more to make it easier to read:
$ env | less
TERM=xterm-256color
LANG=en_US.UTF-8
DISPLAY=:0
GNOME_SHELL_SESSION_MODE=ubuntu
EDITOR=/usr/bin/vi
GTK2_MODULES=overlay-scrollbar
COLORTERM=truecolor
USERNAME=pbmac
JAVA_HOME=/usr/lib/jvm/java-11-oracle
J2SDKDIR=/usr/lib/jvm/java-11-oracle
XDG_VTNR=2
S_COLORS=auto
XDG_SESSION_ID=2
USER=pbmac
[...]
It can appear that there are several ways to set and view variables, and environmental variables in Linux. The table below is to help sort out the differences.
Command | What it does |
---|---|
declare | Declare variables and give them attributes. If no names are given, then display the values of variables instead. |
set | This builtin is so complicated that it deserves its own section. set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. |
env | Without any options, shows current environment variables with their values; However can be used to set environment variable for a single command with the -i flag. |
export | Makes shell variables environment variables. |
Linux PATH Variable
When you type a command into your Linux shell, it doesn't look in every directory to see if there's a program by that name. It only looks to the ones you specify. How does it know to look in the directories mentioned above? It's simple: They are a part of an environment variable, called $PATH, which your shell checks in order to know where to look.
Sometimes, you may wish to install programs into other locations on your computer, but be able to execute them easily without specifying their exact location. You can do this easily by adding a directory to your $PATH. To see what's in your $PATH right now, type this into a terminal:
pbmac@pbmac-server $ echo $PATH
You'll probably see the directories mentioned above, as well as perhaps some others, and they are all separated by colons. Now let's add another directory to the list.
Set your PATH
Let's say you wrote a little shell script called hello.sh and have it located in a directory called /place/with/the/file. This script provides some useful function to all of the files in your current directory that you'd like to be able to execute no matter what directory you're in.
Simply add /place/with/the/file to the $PATH variable with the following command:
export PATH=$PATH:/place/with/the/file
The script anywhere on the system should now be found by just typing in its name, without having to include the full path as you type it.
Set the PATH Permanently
When the computer is restarted or a new terminal instance gets created the addition to the path is gone! This is by design. The variable $PATH is set by the shell every time it launches, but it can be set so that it always includes the new path with every new shell that gets opened. The exact way to do this depends on which shell is being run.
Not sure which shell is running? If you're using pretty much any common Linux distribution, and haven't changed the defaults, chances are you're running Bash. But you can confirm this with a simple command:
pbmac@pbmac-server $ echo $0
That's the "echo" command followed by a dollar sign ($) and a zero. $0 represents the zeroth segment of a command (in the command echo $0, the word "echo" therefore maps to $1), or in other words, the thing running your command. Usually this is the Bash shell, although there are others, including Dash, Zsh, Tcsh, Ksh, and Fish.
For Bash, simply add the line from above - export PATH=$PATH:/place/with/the/file - to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/.bash_profile, ~/.bashrc, or ~/.profile. The difference between these files is (primarily) when they get read by the shell. If you're not sure where to put it, ~/.bashrc is a good choice.
For other shells, you'll want to find the appropriate place to set a configuration at start time; ksh configuration is typically found in ~/.kshrc, zsh uses ~/.zshrc. Check your shell's documentation to find what file it uses.
The alias Command
A Bash alias is a method of supplementing or overriding Bash commands with new ones. Bash aliases make it easy for users to customize their experience in a POSIX terminal. They are often defined in $HOME/.bashrc or $HOME/bash_aliases (which must be loaded by $HOME/.bashrc).
Most distributions add at least some popular aliases in the default .bashrc file of any new user account. These are simple ones to demonstrate the syntax of a Bash alias:
alias ls='ls -F'
alias ll='ls -lh'
Not all distributions ship with pre-populated aliases, though. If you add aliases manually, then you must load them into your current Bash session:
pbmac@pbmac-server $ source ~/.bashrc
Otherwise, you can close your terminal and re-open it so that it reloads its configuration file. To find if any aliases are currently configured, simply use the alias command.
pbmac@pbmac-server $ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ls='ls --color=auto'
So, if you get tired of typing 'ls -al' - you can use alias to create the ll command
pbmac@pbmac-server $ alias ll='ls -al'
pbmac@pbmac-server $ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -al'
alias ls='ls --color=auto'
The time Command
The time command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. 'Real' time is the time elapsed wall clock time taken by a command to get executed, while 'user' and 'sys' time are the number of CPU seconds that command uses in user and kernel mode respectively.
Syntax:
time [ OPTION ] [COMMAND]
To see how much time it takes to run the ls -l command on a specific directory we can use:
pbmac@pbmac-server $ time ls -l > /dev/null
real 0m0.004s
user 0m0.000s
sys 0m0.004s
Meaning the real clock time was 0.004 seconds, 0.0 seconds running in user space, and 0.004s running in system space.
Adapted from:
"env command in Linux with Examples" by Prasoon_Mishra, Geeks for Geeks is licensed under CC BY-SA 4.0
"env" by Multiple Contributors, Wikipedia is licensed under CC BY-SA 4.0
"How to set your $PATH variable in Linux" by Jason Baker, OpenSource.com is licensed under CC BY-SA 4.0
"time command in Linux with examples" by DrRoot_, Geeks for Geeks is licensed under CC BY-SA 4.0