1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in t
ID: 3605289 • Letter: 1
Question
1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the
terminal using the user name of root and the password of LNXrocks!.
2. At the command prompt, type set | less and press Enter to view the BASH shell
environment variables currently loaded into memory. Scroll through this list using the
cursor keys on the keyboard. When finished, press q to quit the less utility.
3. At the command prompt, type env | less and press Enter to view the exported BASH
shell environment variables currently loaded into memory. Scroll through this list
using the cursor keys on the keyboard. Is this list larger or smaller than the list
generated in Step 2? Why? When finished, press q to quit the less utility.
4. At the command prompt, type PS1="Hello There:" and press Enter. What happened
and why? Next, type echo $PS1 at the command prompt and press Enter to verify the
new value of the PS1 variable.
5. At the command prompt, type exit and press Enter to log out of the shell. Next, log in
to the terminal using the user name of root and the password of LNXrocks!. What
prompt did you receive and why? How could you ensure that the “Hello There: “
prompt occurs at every login?
6. At the command prompt, type vi.bash_profile and press Enter. At the bottom of
the file, add the following lines. When finished, save and quit the vi editor.
echo –e "Would you like a hello prompt? (y/n) -->c"
read ANSWER
if [ $ANSWER = "y" ]
then
PS1="Hello There: "
fi
Explain what the preceding lines will perform after each login.
7. At the command prompt, type exit and press Enter to log out of the shell. Next, log in to
the terminal using the user name of root and the password of LNXrocks!. When prompted
for a hello prompt, type y and press Enter. What prompt did you receive and why?
8. At the command prompt, type exit and press Enter to log out of the shell. Next, log in
to the terminal using the user name of root and the password of LNXrocks!. When
prompted for a hello prompt, type n and press Enter to receive the default prompt.
9. At the command prompt, type MYVAR=“My sample variable” and press Enter to
create a variable called MYVAR. Verify its creation by typing echo $MYVAR at the
command prompt, and press Enter.
10. At the command prompt, type set | grep MYVAR and press Enter. Is the MYVAR
variable listed? Why?
11. At the command prompt, type env | grep MYVAR and press Enter. Is the MYVAR
variable listed? Why?
12. At the command prompt, type export MYVAR and press Enter. Next, type env | grep
MYVAR at the command prompt and press Enter. Is the MYVAR variable listed now? Why?
13. At the command prompt, type exit and press Enter to log out of the shell. Next, log in
to the terminal using the user name of root and the password of LNXrocks!.
14. At the command prompt, type echo $MYVAR and press Enter to view the contents of
the MYVAR variable. What is listed and why?
15. At the command prompt, type vi.bash_profile and press Enter. At the bottom of
the file, add the following line. When finished, save and quit the vi editor.
export MYVAR=“My sample variable”
16. At the command prompt, type exit and press Enter to log out of the shell. Next, log in
to the terminal using the user name of root and the password of LNXrocks!.
17. At the command prompt, type echo $MYVAR and press Enter to list the contents of the
MYVAR variable. What is listed and why?
18. At the command prompt, type alias and press Enter.What aliases are present in your shell?
19. At the command prompt, type alias asample=“cd /etc ; cat hosts ; cd ~ ; ls
–F” and press Enter. What does this command do?
20. At the command prompt, type asample and press Enter. What happened and why?
What environment file could you add this alias to such that it is executed each time
a new BASH shell is created?
21. Type exit and press Enter to log out of your shell.
342 Chapter 7 Working with the BASH Shell
Copyright
Explanation / Answer
2)
set command is used for setting the shell variables. It can also be used to list the current shell variables.
3)
env stands for environment. This command can be used for listing all the environment variables and it can be used for running a program in a customised environment, without affecting the current environment.
4)
On running the command PS1="Hello There" the linux command shell prompt changes to the string we have mentioned here ie; "Hello There". PS1 is a shell variable which stores the value of the linux command shell prompt.
5)
No we won't be having the same command shell prompt. The value of the shell variable PS1 will be reset to it's original value. To ensure that we get"Hello There" as our command shell prompt, the command PS1="Hello There" should be written to the file .bashrc which is present in the home directory of every user. This file gets executed whenever the terminal is accessed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.