Intro to Bash and the Linux Shell

Presented to the Sioux City Linux Users Group
By Ryan Stille

In most installations, as soon as you get Linux installed, you get a nice graphical interface and rarely if ever need to make use of the so-called terminal mode (aka shell prompt).

However, in Linux the simple, modest terminal is not merely an afterthought, but an extremely powerful tool. While it may be true that you don’t need to use it, it’s not that difficult to learn, and very useful to know. 

  1. > >Invoking Bash

  2. > >The Bash prompt

  3. > >Basics: Moving around and listing files
    Here are some basic Linux commands and their DOS counterparts

    1. > >DIR  = ls

    2. > >CD  = cd  (or pwd to see current directory)

    3. > >COPY  = cp

    4. > >MOVE  = mv

    5. > >DEL  = rm

    6. > MKDIR =  mkdir

    7. > RMDIR = rmdir

    8. > >REN  = mv

    9. > >TYPE  = cat
  4. > Some programs can have options passed to them

    1. > >ls -l >  (the l stands for the >long > format)

    2. > Some options can be a word or words instead of a letter

      1. > >–full-time >  (show files with a full timestamp)
    3. > Some options can have a value

      1. > >–format=long >  (same as -l )
  5. > >Passing parameters to programs

    1. > >ls -l /tmp

    2. > >ls -l /tmp/file1.txt /tmp/file2.txt
  6. > >Aliases

    1. > >Aliases  are kind of like shortcuts

    2. > Can be setup in the bashrc file

    3. > If you want to setup some default aliases available to all users, set them in /etc/bashrc

    4. > >ls example

    5. > >cp, mv, and rm are good candidates for aliases
  7. > Obtaining help

    1. > man  (stands for >manual >)

      1. > To pull up the ‘man page’ for the ls command, use >man ls

      2. > Use man man to learn how to use the man command, but I will tell you that you can use the Page Up and Page down keys, and use q to exit  (Also note that >q > is used to exit a lot of unix programs)
    2. > info  Type >info info > to learn how to use the info command.  Most documentation is written for use with the info command, and then a man page also generated from this data.  Complex programs require complex documentation, and info handles this much better than man.

    3. > The –help option.  Many programs will give you a short description and a list of options when you pass them –help.  try >ls –help

    4. > Online - the Linux Documentation Project offers many man and info pages, plus a lot more like HowTo guides and general documentation on various subjects.
  8. > Wildcards - * and ? in Linux work similar to how they do in DOS

    1. > You can also use square braces ([ ]) to specify a set or range of characters

    2. > >[abc]* > matches any file that begins with a, b, or c

    3. > >file[1-5].txt > matches file1.txt, file3.txt, and file5.txt, but not file7.txt
  9. > Redirecting output

    1. > The redirect operator “>” redirects output to a file.  The file will be created if it doesn’t exist, or replaced if it does.

    2. > Use “>>” to append instead of overwrite.  The file will still be created if it doesn’t exist.
  10. > Piping output

    1. > Use the pipe (”|”) to redirect (or ‘pipe’) output to another program
  11. > >Other useful commands

    1. > more

    2. > sort

    3. > w

    4. > df

    5. > grep

    6. > exit, logout, Ctrl+D


Advanced Techniques

(or how to be a Linux Guru)

  1. > Handy environment variables

    1. > OLDPWD

    2. > HOME  (You can also use the ~ character to represent your home dir)
  2. > Bash tricks

    1. > the TAB key  (my most time saving Bash trick)

    2. > Uparrow/downarrow keys

    3. > Ctrl+R reverse command search

    4. > >Use the >history > command to see previously run commands

      1. > U >se >! > to run a command
    5. > Use >!! > to run the last command you entered

    6. > Use >!abc > to run the last command you entered that started with abc

    7. > Use >!$ > to reference the last ‘word’ of the previous command

    8. > Use >!* > to reference all the args and parameters of the previous command

    9. > You can use >:p > at the end of these to have Bash print the command it will run instead of running it

    10. > Typos

      1. >  You can run >shopt -s cdspell > to have Bash try to correct typos in directories when you are ‘cd’ing to them

      2. > To fix a typo in the last command you ran, use >^error^correction

      3. > >Use >fc > ( >f >ix >c >ommand) to modify the last command you entered in your text editor.  When you quit the editor, the command will be run.

      4. > Use fc -n x y to edit commands from lines x to y in your history
    11. > Moving around on the command line

      1. > Ctrl+A places the cursor at the beginning of the line

      2. > Ctrl+E places the cursor at the end of the line

      3. > Ctrl+W cuts the word in front of the cursor

      4. > Ctrl+U cuts everything in front of the cursor
    12. > You can set Bash to automatically log you out after >x > seconds

      1. > >export TMOUT=300 >  This is good for secure terminals that root users may leave unattended
    13. > Pausing and starting processes

      1. > To pause a running process and put it into the background, use Ctrl+Z

      2. > To start it again and bring it into the foreground, use >fg

      3. > To start it again but leave it in the background use bg
    14. > You can scroll up and down on your terminal by using Shift+PgUp and Shift+PgDown

Continuing Your Education 

  • > http://www.ctssn.com/  -  Linux tutorial site

  • > http://rute.sourceforge.net/  -  Online version of a Linux tutorial book

  • > Search for linux tutoral on your favorite search engine

  • > Ask questions on the SLUG mailing list

Originally published 04/13/02.

Comments are closed.