Set Variables
Linux Commands - Shells

The shell gives you the option to set variables for PATH, HISTORY, etc.

The pathname is a string of text that  indicates the location of a file or folder.  There is a variable for this called PATH.    As you'll see, this one makes your life a lot easier.

Lesson 8 / Lesson 10

If it weren't for the path variable, you'd have to type in a complete path--including all directories and subdirectories--for every command you enter on the command-line.  So, if you wanted to use the "less" utility to look at the contents of "newtext.txt", you'd have to enter:

      /usr/bin/less newtext.txt

The path variable tells the shell to look in certain directories to find the executable files that make your programs and system utilities work.  With the path variable set, you can abbreviate the above command to:

         less newtext.txt


You can use the echo command to see how the path variable is set.

        echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/games


Note that there are several separate paths in this statement.  Each path is separated from the next by a colon.  You can invoke an executable in any one of these paths without having to enter the entire path on the command-line.  But, what if you want to invoke an executable that's not in any of these paths?

You can invoke the command by entering the entire path, as we did in the above example with the less utility.  Or, you can cd to the directory where the program executable is located, and prefix the command with a "./".

     cd BOINC/
     BOINC]$ ./run_manager


The "./" tells the shell to look in the current working directory for the program executable that's being invoked.

Note:  In the DOS/Windows world, the current working directory is an implied part of the path variable.  So, if you're in the directory where the program executable that you want to invoke is stored, you don't need to prefix the command with anything.  In the Linux world, the current working directory isn't implied as part of the path variable, so you need to implicitly tell the system to look there when you want to invoke a program.  This was purposely set up this way for security reasons.

Finally, you can use the export command to add a new path to the path variable:

     export PATH=$PATH:/opt/bin
     echo $PATH
    /usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache/bin:/usr/local/sbin:/usr/local/bin:/sbin:
        /bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/lib/jre/bin:/root/bin:/usr/lib/jre/bin:/opt/bin
    

Note how the new path is added at the end of the path statement.

Hash Table
When a command is executed without a path, the shell searches the PATH variable to locate it.  When it is found it places that in a hash table.  After that Bash checks the hash table first.

There are several other environment variables that may be set on your system.  One of the important aspects of the environment is that when the parent process spawns a child, the child and any applications that start from the child process will inherit the environment from the parent.

parent process


You can see what all of them are by issuing the "env" command.

     env
    SSH_AGENT_PID=4354
    HOSTNAME=localhost.localdomain
    SHELL=/bin/bash
    TERM=xterm
    HISTSIZE=1000
    GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
    WINDOWID=41943208
    OLDPWD=/etc
    USER=donnie
    LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:
          mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:
          *.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31
          :*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:
          *.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:
          *.tif=00;35:
    GNOME_KEYRING_SOCKET=/tmp/keyring-0xU7ub/socket
    SSH_AUTH_SOCK=/tmp/ssh-hDkQIP4294/agent.4294
    KDEDIR=/usr
    SESSION_MANAGER=local/localhost.localdomain:/tmp/.ICE-unix/4294
    MAIL=/var/spool/mail/donnie
    DESKTOP_SESSION=default
    PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache/bin:/usr/local/sbin:/usr/local/bin:
          /sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/lib/jre/bin:/root/bin:/usr/lib/jre/bin:
          /opt/bin
    INPUTRC=/etc/inputrc
    PWD=/
    LANG=en_US.UTF-8
    GDMSESSION=default
    SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
    HOME=/home/donnie
    SHLVL=2
    GNOME_DESKTOP_SESSION_ID=Default
    LOGNAME=donnie
    DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ovZR0qXh6n
    LESSOPEN=|/usr/bin/lesspipe.sh %s
    DISPLAY=:0.0
    G_BROKEN_FILENAMES=1
    COLORTERM=gnome-terminal
    XAUTHORITY=/home/donnie/.Xauthority
    _=/bin/env
    

Or, use the echo command if you just want to see the value of one variable.  (It'll work the same way as it did in the previous examples.)

    echo $USER
   tom
    
Here's a table of some of the more common environment variables.

USER
    The username of the person who is currently logged on to the system.
SHELL
    The path to the shell that is currently in use.
PWD
    The current working directory.
HOSTNAME
    This is the TCP/IP hostname of the computer.
PATH
    A colon-delimited list of directories in which the system looks when you type an executable program name.  This variable gets built in several configuration files, such as /etc/profile and the .bashrc file that resides in the user's home directory.
HOME
    This holds the path for the current user's home directory.  Some programs will use this variable to determine where to find configuration files or determine the default location to store files.
LD_LIBRARY_PATH
    It works like the path variable.  Some programs will use it to help find their library files.
PS1
    The default bash prompt.
NNTPSERVER
    Some newsreader programs may use this variable to set the name of the news server.
TERM
    This specifies the current terminal type.  You'll probably see it set as "xterm".  The system needs to know which terminal type is in use so that it will know how to move the cursor and display text effects in text-mode programs.
DISPLAY
    This variable allows you to have multiple displays running from the same computer.  If you're only running one display, you'll see a returned value of ":0.0".  (That means, the first display on the current computer.)
EDITOR
    This sets the default text editor that you want to use.  You have several choices, but for best results, always set it to use a text-mode editor.  Some good choices would be vi, joe, or emacs.

The history Command


The history command remembers the last 1000 commands that you have performed at the command line.  The value of the history command is that you can go back and review a command that you used in case you do not remember or you can see where you made a mistake.  
---cut---
   558  ls /usr/sbin
  559  lsusb
  560  su - diane
  561  ls /usr/sbin
  562  ifconfig
  563  safe-finger
  564  finger tom
  565  ifdown eth0
  566  ap-get update
  567  apt-get update
  568  history

The history is listed from the last 1000 commands (500 for Ubuntu) starting with the furthest back as number 1 and the most recent as number 1000.  If you have not run 1000 commands you will see that it will list the number that you have used.  

If you pipe the history command to the command less you may scroll backwards and forwards by using the arrow keys.  The | symbol pipes the output of one command into another.

history | less

This history is kept in the hidden file .bash_history in your home directory.  There are several configurable variables that you may want to adjust.


History Variables:
HISTCMD            history number of current command
HISTFILE            history command saves data here ~/.bash_history
HISTFILESIZE        max number of lines stored in history
HISTSIZE            max number of commands to remember in history

View these variables by using the echo command:

echo $HISTFILE
/home/mike/.bash_history

echo $HISTSIZE
500
echo $HISTCMD
1005
echo $HISTFILESIZE
1000

You may change the history file size with this command:

HISTFILE=1200

That would change the history file size to 1200 instead of 500.

You can also review your past commands using the up and down arrow keys on your keyboard to call-up previously entered commands.  If you keep pressing the up arrow key, you'll scroll through the list of previous commands, starting with the last one entered.  If you go past the command that you want, you can use the down arrow key to get back to it.  When you finally do get to the command that you want to repeat, you can either press the Enter key to enter it as is, or edit it and then press Enter.

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874