Docsity

chap1 practice question, Cheat Sheet of Linux skills

Toggle sidebar
bg1
Bash Practice Questions for Chapter 1: Create and Run Your First BashBash Practice Questions for Chapter 1: Create and Run Your First Bash
Shell ScriptShell Script
These simple exercises will help you practice wha t you learned in the first chapter of the Bash Beginner Series on Linux Handbook.
Exercise 1: Who are you?Exercise 1: Who are you?
Write a shell script in that prints your user name.
Difficulty level: Easy
Hint: Use echo command with USER environm ent variable.
Exercise 2: Where are you?Exercise 2: Where are you?
Write a shell script in that prints your current workin g directory.
Difficulty level: Easy
Hint: Use echo command with PWD environm ent variable.
Exercise 3: Where is your home?Exercise 3: Where is your home?
Write a shell script in that prints the absolute path to your home directory.
Difficulty level: Easy
Hint: Use echo command with HOME environ ment variable.
Exercise 4: What is your default shell?Exercise 4: What is your default shell?
Write a shell script in that prints the default shell.
Difficulty level: Easy
Hint: Use echo command with SHELL environ ment variable.
Exercise 5: Tell me all about youExercise 5: Tell me all about you
Write a shell script in that prints all of the above four questions, i.e. your user name, current working directory, home directory and default sh ell. Print all the answers in
a separate line.
Difficulty level: Easy
To make things interesting, your output should be like this:
Hello
My name is XYZ
My current location is XYZ
My home directory is XYZ
My default shell is XYZ
You have to replace XYZ with actual values, of cou rse.
pf2

Partial preview of the text

Download chap1 practice question and more Cheat Sheet Linux skills in PDF only on Docsity!

Bash Practice Questions for Chapter 1: Create and Run Your First BashBash Practice Questions for Chapter 1: Create and Run Your First Bash

Shell ScriptShell Script

These simple exercises will help you practice what you learned in the first chapter of the Bash Beginner Series on Linux Handbook.

Exercise 1: Who are you?Exercise 1: Who are you?

Write a shell script in that prints your user name. Difficulty level: Easy Hint: Use echo command with USER environment variable.

Exercise 2: Where are you?Exercise 2: Where are you?

Write a shell script in that prints your current working directory. Difficulty level: Easy Hint: Use echo command with PWD environment variable.

Exercise 3: Where is your home?Exercise 3: Where is your home?

Write a shell script in that prints the absolute path to your home directory. Difficulty level: Easy Hint: Use echo command with HOME environment variable.

Exercise 4: What is your default shell?Exercise 4: What is your default shell?

Write a shell script in that prints the default shell. Difficulty level: Easy Hint: Use echo command with SHELL environment variable.

Exercise 5: Tell me all about youExercise 5: Tell me all about you

Write a shell script in that prints all of the above four questions, i.e. your user name, current working directory, home directory and default shell. Print all the answers in a separate line. Difficulty level: Easy To make things interesting, your output should be like this: Hello My name is XYZ My current location is XYZ My home directory is XYZ My default shell is XYZ You have to replace XYZ with actual values, of course.

Solutions to the ExercisesSolutions to the Exercises

Solution 1: Who are you?Solution 1: Who are you?

Write a shell script in that prints your user name. #!/bin/bash echo "$USER" This $USER is one of the environment variable which is already set in almost all Linux distributions. Note that while the quotes are not important here, it is always a good practice to use double quotes with echo. More on the use of quotes can be found here.

Solution 2: Where are you?Solution 2: Where are you?

Write a shell script in that prints your current working directory. #!/bin/bash echo "$PWD"

Solution 3: Where is your home?Solution 3: Where is your home?

Write a shell script in that prints the absolute path to your home directory. #!/bin/bash echo "$HOME"

Solution 4: What is your default shell?Solution 4: What is your default shell?

Write a shell script in that prints the default shell. #!/bin/bash echo "$SHELL"

Solution 5: Tell me all about youSolution 5: Tell me all about you

Write a shell script in that prints all of the above four questions. #!/bin/bash echo "Hello" echo "My name is $USER" echo "My current location is $PWD" echo "My home directory is $HOME" echo "My default shell is $SHELL"