Skip to main content
Engineering LibreTexts

13-A.2: env / alias / PATH / time

  • Page ID
    43160
  • 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

    • Was this article helpful?