1.2 Hello World!
Learning objectives
- Recall bash commands to manipulate strings (
echo
,ls
,cat
) - Understand how output redirection works (
>
)
A Hello, World! is a minimalist example that is meant to demonstrate the basic syntax and structure of a programming language or software framework. The example typically consists of printing the phrase 'Hello World!' to the output, such as the console or terminal, or writing it to a file.
Let's demonstrate this with simple commands that you can run directly in the terminal.
1.2.1 Printing a string
The echo
command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file.
The most straightforward usage of the echo
command is to display text or a string on the terminal. To do this, you simply provide the desired text or string as an argument to the echo
command:
Exercise
Use the echo
command to print the string 'Hello World!'
to the terminal.
1.2.2 Redirecting outputs
The output of the echo
can be redirected to a file instead of displaying it on the terminal. You can achieve this by using the >
operator for output redirection.
Exercise
Use the >
operator to redirect the output of echo to a file named output.txt
.
1.2.3 Listing files
The Linux shell command ls
lists directory contents of files and directories. It provides valuable information about files, directories, and their attributes.
ls
will display the contents of the current directory:
Exercise
List the files in the working directory to verify output.txt
was created.
1.2.4 Viewing file contents
The cat
command in Linux is a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents.
The most basic use of cat
is to display the contents of a file on the terminal. This can be achieved by simply providing the filename as an argument:
Exercise
Use the cat
command to print the contents of output.txt
.
In preparation for the following lessons, delete output.txt
.
Summary
This lesson recalls basic bash commands using a classic "Hello World!" example, including:
- Printing to the terminal with
echo
- Redirecting output to a file with
>
- Viewing files and file contents with
ls
andcat