w3resource

Bash Basic Commands: Exercises and Solutions

1.

Echo Command Basics:

Write a Bash script that uses the echo command to print "Hello, World!" to the terminal.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to print "Hello, World!" to the terminal

echo "Hello, World!"
# Echo command: Prints the string "Hello, World!" to the terminal

Output:

Hello, World!

Explanation:

In the exercise above,

  • #!/bin/bash: This is called a shebang line. It tells the system which interpreter to use for executing the script. In this case, it specifies that the script should be interpreted using the Bash shell (/bin/bash).
  • echo "Hello, World!": This is the actual command that prints "Hello, World!" to the terminal. The echo command is used to display messages or variables on the terminal.

2.

Echo with Variables:

Write a bash script that takes a user's name as input and uses the echo command to greet the user with "Hello, [name]!".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to greet the user with their name

# Prompt the user to enter their name
echo "Input your name:"

# Read user input and store it in a variable named 'name'
read name

# Greet the user with their name
echo "Hello, $name!"

Output:

Input your name:
Ethniu
Hello, Ethniu! 

Explanation:

In the exercise above,

The script prompts the user to input their name, reads the input, and then greets the user with "Hello, [name]!", where [name] is replaced with the input provided by the user.

3.

Cat Command Usage:

Write a Bash script that uses the cat command to display the contents of a text file named "exec_stdout.txt".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to display the contents of a text file named "sample.txt" using the cat command

# Displaying the contents of the file named " sample.txt"
cat sample.txt
# Cat command: Prints the contents of the specified file (in this case, " sample.txt") to the terminal

Output:

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. The shell's name is an acronym for Bourne-Again SHell, a pun on the name of the Bourne shell that it replaces and the notion of being "born again".

Explanation:

In the exercise above,

cat sample.txt: This line uses the "cat" command to display the contents of the text file named "exec_stdin.txt" to the terminal. The "cat" command concatenates and displays the content of one or more files.

4.

Listing Files:

Write a Bash script that uses the ls command to list all files and directories in the current directory.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to list all files and directories in the current directory using the ls command

# Listing all files and directories in the current directory
ls
# ls command: Lists the files and directories in the current directory

Output:

exec_command
exec_stderr.txt
exec_stdin.txt
exec_stdout.txt
Main.sh
main.tar.gz
runner_mysql
runner_php
run_user

Explanation:

In the exercise above,

The script simply executes the "ls" command, which lists all files and directories in the current directory and displays them in the terminal when the script is run.

5.

Echo with Escape Sequences:

Write a Bash script that uses the echo command to print a multi-line message using escape sequences for newlines (\n) and tabs (\t).

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to print a multi-line message using escape sequences

# Using echo command to print a multi-line message
echo -e "This is Exercise-1.\n\tThis is Exercise-2.\n\t\tThis is Exercise-3."
# -e option enables interpretation of backslash escapes
# \n represents a newline character
# \t represents a tab character

Output:

This is Exercise-1.
	This is Exercise-2.
		This is Exercise-3.

Explanation:

In the exercise above,

echo -e "This is Exercise-1.\n\tThis is Exercise-2.\n\t\tThis is Exercise-3.": This line uses the echo command with the -e option to enable interpretation of backslash escapes.

  • \n represents a newline character, causing subsequent text to be printed on a new line.
  • \t represents a tab character, causing subsequent text to be indented with a tab.
  • The message "This is Exercise-1." is printed on the first line, "This is Exercise-2." is printed on the second line with a tab indentation, and "This is Exercise-3." is printed on the third line with two tabs indentation.

6.

Echo with Command Substitution:

Write a Bash script that uses command substitution to display the current date and time using the date command.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to display the current date and time using command substitution

# Using command substitution to get the current date and time
current_datetime=$(date)

# Displaying the current date and time
echo "Current date and time: $current_datetime"

Output:

Current date and time: Fri 12 Apr 2021 06:11:36 AM UTC

Explanation:

In the exercise above,

The script executes the "date" command inside command substitution $(date) to obtain the current date and time. The result is stored in the variable 'current_datetime', and then it is displayed using the "echo" command along with a message. When we run this script, it will output the current date and time.

7.

Cat with File Concatenation:

Write a Bash script that uses the cat command to concatenate the contents of two text files ("file1.txt" and "file2.txt") and display the result.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to concatenate the contents of two text files and display the result

# Using cat command to concatenate the contents of two text files and display the result
cat file1.txt file2.txt
# Cat command: Concatenates the contents of the specified files ("file1.txt" and "file2.txt") and displays the result

Output:

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. The shell's name is an acronym for Bourne-Again SHell, a pun on the name of the Bourne shell that it replaces and the notion of being "born again".
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. The shell's name is an acronym for Bourne-Again SHell, a pun on the name of the Bourne shell that it replaces and the notion of being "born again".

Explanation:

In the exercise above,

The script simply executes the "cat" command with the filenames "file1.txt" and "file2.txt" as arguments. The "cat" command concatenates the contents of these files and displays the results in the terminal when the script is run.

8.

Echo with Output Redirection:

Write a Bash script that redirects the output of the echo command to a file named "output.txt" instead of displaying it on the terminal.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to redirect the output of the echo command to a file

# Redirecting the output of the echo command to a file named "output.txt"
echo "Hello, World!" > output.txt
# Echo command: Prints the string "Hello, World!" to the file "output.txt" instead of displaying it on the terminal

Explanation:

In the exercise above,

The > operator is used to redirect the output of the "echo" command to the file named "output.txt". When the script is executed, the string "Hello, World!" will be written to the file "output.txt" instead of displayed on the terminal.

9.|

Using ls with Options:

Write a Bash script that uses the ls command with the -l option to display detailed information about files and directories in the current directory.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to display detailed information about files and directories in the current directory using ls -l

# Using ls command with -l option to display detailed information
ls -l
# ls command: Lists files and directories in the current directory
# -l option: Displays detailed information including permissions, owner, group, size, and modification date

Output:

-rwxr-xr-x 1 ubuntu ubuntu    13 Apr 12 07:04 exec_command
-rw-r--r-- 1 root   root       0 Apr 12 07:04 exec_stderr.txt
-rw-rw-r-- 1 ubuntu ubuntu     6 Apr 12 07:04 exec_stdin.txt
-rw-r--r-- 1 root   root       0 Apr 12 07:04 exec_stdout.txt
-rw-r--r-- 1 ubuntu ubuntu   448 Jan  1  2000 Main.sh
-rw-rw-r-- 1 ubuntu ubuntu   329 Apr 12 07:04 main.tar.gz
-rwxrwxr-x 1 ubuntu ubuntu   522 Apr 12 07:04 runner_mysql
-rwxrwxr-x 1 ubuntu ubuntu   229 Apr 12 07:04 runner_php
-rwxrwxr-x 1 ubuntu ubuntu 21552 Apr 12 07:04 run_user

Explanation:

In the exercise above,

The "ls" command is used with the -l option, which instructs "ls" to display detailed information about each file and directory in the current directory. When the script is executed, it will output the detailed information for each item in the directory.

10.

Echo with Formatting:

Write a Bash script that uses the echo command to display a message with formatted text, such as bold or colored text, using ANSI escape codes.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Script to display a message with formatted text using ANSI escape codes

# Define ANSI escape codes for formatting
bold=$(tput bold)
red=$(tput setaf 1)
reset=$(tput sgr0)

# Using echo command to display a message with formatted text
echo -e "${bold}${red}This is a bold and red message.${reset}"
# -e option enables interpretation of backslash escapes for ANSI escape codes

Output:

This is a bold and red message.

Explanation:

In the exercise above,

  • bold=$(tput bold): This line defines a variable named 'bold' and assigns it the escape sequence obtained from the "tput bold" command. "tput bold" retrieves the escape sequence for making text bold.
  • red=$(tput setaf 1): This line defines a variable named 'red' and assigns it the escape sequence obtained from the "tput setaf 1" command. "tput setaf 1" retrieves the escape sequence for setting text color to red.
  • reset=$(tput sgr0): This line defines a variable named 'reset' and assigns it the escape sequence obtained from the "tput sgr0" command. "tput sgr0" retrieves the escape sequence for resetting text formatting and color back to default.
  • echo -e "${bold}${red}This is a bold and red message.${reset}": This line uses the "echo" command to display a message with formatted text.
    • ${bold} and ${red} are variable substitutions that insert escape sequences for "bold text" and "red text", respectively.
    • "This is a bold and red message." is the message to be displayed.
    • ${reset} is a variable substitution that inserts the escape sequence for resetting text formatting and color back to default.
    • The -e option enables interpretation of backslash escapes, allowing ANSI escape codes to be recognized and applied correctly.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.