Lab 1: Working with Bash

Lab 1: Working with Bash

CIS 218LAB #1Advanced UNIX

Concepts: environment variables, aliases, redirection, pipes, repeating commands, editing command line, login scripts

Instructions: Capture the results of your terminal session. Print it out with your name, class and section.

  1. Log on and open a terminal window. Confirm that you're running bash with the ps command. If for some reason you are not running bash, use the chsh command to set your default login shell to bash, then log out and log back in.
  2. Run the command that gives you a list of all the environment variables that are currently set.
  3. Using a pipe to the wc command, find out how many environment variables are currently set. Answer Question 1.
  4. Create a new environment variable called MYVAR and set its value to "myvalue". Use the command export MYVAR to allow MYVAR to be used by sub-programs of the shell. Verify that this step worked by printing out the value of MYVAR on the terminal. Also repeat step 3 to ensure that the number of variables increased by 1.
  5. Try changing your command prompt to:
  6. "prompt> "
  7. nothing
  8. your username
  9. the output of the date command followed by "$ ". Answer Question 2.
  10. Now try changing the command prompt in this way: PS1=”\d \t $ “. Answer Question 3.
  11. There is a program in /cispublic called hello. Try running this program from your home directory by typing hello at the prompt (don’t copy it to your home directory first). This won't work; you need to use the absolute path to run the program - try this now. Now change the environment variable called PATH so that you can run hello without an absolute path from anywhere. Answer Question 4.
  12. Create some aliases for yourself. Some suggestions are below, but also think of one or two more that might be useful to you.
  13. 'll' for 'ls -l'
  14. 'la' for 'ls -la | less'
  15. 'psall' for 'ps -ef'
  16. 'up' for 'cd ..'
  17. 'back' for 'cd -'
  18. Now open a new terminal window from the desktop menu. Check some of the environment variables and aliases you changed or created in steps 4-8. They don't work in this new shell. You need to place these commands in a login script in order for them to take effect whenever you start a new shell.
  19. First, use the history command to list the recent commands you have issued. Figure out in what range of command numbers your environment variable and alias commands fall, and use the fc -l command to list only those commands.
  20. IMPORTANT: In your home directory, make a backup of your .bashrc file (cp .bashrc .bashrc-original).
  21. Make sure you are in your home directory. Now run the same fc command as in step 10, but append its output to .bashrc.
  22. Edit .bashrc (using vi) to remove the command numbers and any extraneous commands. Also add the line echo ".bashrc was read" to this file. Save and quit.
  23. Now start a new terminal window from the desktop menu - were your commands executed? What about when you start a new shell from the old one (by typing bash on the command line)? What about when you ssh to csc? Answer Question 5.
  24. In your home directory, there should also be a file called .bash_profile. Open this file and add the line echo ".bash_profile was read" to it. Now experiment to find out when this file is used.
  25. when you open a new terminal window?
  26. when you start a new shell within an existing one?
  27. When you ssh to another machine? Answer Question 6.
  28. Clean up your .bashrc and .bash_profile files by removing the echo commands we used in steps 13 and 15, and by removing/changing/adding environment variables and aliases, based on your personal preferences. If you made a real mess of .bashrc, you can restore the original one using the copy you made in step 11.

Questions

  1. 1.How many environment variables were originally set when you logged in?
  2. 2.What command did you use to set the prompt to the output of the date command followed by "$ "?
  3. 3.Other than the fact that the timezone and year are no longer displayed, how is the prompt you obtained in step 6 different from the last one you got in step 5? Why?
  4. 4.What command did you use to modify your PATH so you could run hello from any directory without typing the absolute path?
  5. 5.When is .bashrc used?
  6. 6.When is .bash_profile used?