Popular Searches

Edit Text Files and Shell Environment Settings

LinuxBeginner
Practice Now

Introduction

Administrators often need to create text files, edit configuration values, and apply shell environment settings. RHCSA-style work may not require a specific editor, but it does require that you understand what changed and how to verify the result.

In this lab, you will work directly inside /home/labex/project. The setup prepares one application configuration file. You will back it up, create a new message file with a here-document, edit key-value settings with sed, create a project environment file, and use that environment file to drive a final workspace operation.

Inspect and Back Up a Configuration File

In this step, you will inspect a text configuration file before editing it. A backup gives you a known copy of the original state, which is a basic safety habit before changing configuration.

Move into the standard LabEx project directory.

cd /home/labex/project

List the prepared configuration file.

ls -l configs/app.conf

Read the file. It uses simple KEY=value lines, a common style for Linux application configuration.

cat configs/app.conf

Create a backup copy before editing. The backup stays in the same directory with the .bak suffix.

cp configs/app.conf configs/app.conf.bak

Compare the original file and the backup. A successful cmp command prints nothing, which means the files are identical.

cmp configs/app.conf configs/app.conf.bak

Create a Text File with a Here-Document

In this step, you will create a new text file from the shell. A here-document starts after <<'EOF' and ends at a line that contains only EOF. This is useful for writing a small multi-line file without opening an interactive editor.

Start from the project directory.

cd /home/labex/project

Create an administrator message file. The quoted EOF marker keeps the text literal, so the shell does not expand variables inside the message.

cat > configs/admin-message.txt <<'EOF'
Inventory service maintenance window
Owner: platform-team
Status: planned
EOF

Read the file back. You should see the three lines you entered.

cat configs/admin-message.txt

Count the lines to confirm the file has exactly three lines.

wc -l configs/admin-message.txt

Edit Configuration Values with sed

In this step, you will edit existing configuration lines in place. The sed -i command changes a file directly, so you should use focused patterns that match only the intended lines.

Start from the project directory.

cd /home/labex/project

Change LOG_LEVEL from info to debug. The pattern starts with ^ and ends with $ so it matches the full line only.

sed -i 's/^LOG_LEVEL=info$/LOG_LEVEL=debug/' configs/app.conf

Change RETRY_LIMIT from 2 to 4.

sed -i 's/^RETRY_LIMIT=2$/RETRY_LIMIT=4/' configs/app.conf

Enable cache by changing ENABLE_CACHE=false to ENABLE_CACHE=true.

sed -i 's/^ENABLE_CACHE=false$/ENABLE_CACHE=true/' configs/app.conf

Read the edited file.

cat configs/app.conf

Compare the edited file to the backup. The files should now differ, so diff prints changed lines. The || true part keeps the shell command from stopping only because diff found differences.

diff -u configs/app.conf.bak configs/app.conf || true

Create and Source a Project Environment File

In this step, you will create a shell environment file and load it with source. Environment files are a safe way to keep project-specific variables together without changing system-wide settings.

Start from the project directory.

cd /home/labex/project

Create rhcsa-env.sh. The export command marks variables so commands launched from this shell can read them.

cat > rhcsa-env.sh <<'EOF'
export RHCSA_APP_NAME=inventory
export RHCSA_MODE=training
export RHCSA_WORKDIR=/home/labex/project/workspace
EOF

Read the environment file before loading it.

cat rhcsa-env.sh

Load the file into the current shell. source runs the file in the current shell process, so the exported variables become available immediately.

source rhcsa-env.sh

Print the variables to confirm they are available.

echo "$RHCSA_APP_NAME"
echo "$RHCSA_WORKDIR"

Use Environment Settings for a Workspace Task

In this step, you will use the variables from rhcsa-env.sh to complete a small workspace task. A new shell does not automatically know project variables, so source the file again before using them.

Start from the project directory.

cd /home/labex/project

Load the project environment file.

source rhcsa-env.sh

Create the workspace directory named by RHCSA_WORKDIR.

mkdir -p "$RHCSA_WORKDIR"

Copy the edited configuration into that workspace.

cp configs/app.conf "$RHCSA_WORKDIR/${RHCSA_APP_NAME}.conf"

Store the current training mode in the workspace. printf is used here because it prints exactly the text you provide.

printf '%s\n' "$RHCSA_MODE" > "$RHCSA_WORKDIR/mode.txt"

List the workspace and read the mode file.

ls -l "$RHCSA_WORKDIR"
cat "$RHCSA_WORKDIR/mode.txt"

Summary

You practiced creating and editing text files in a beginner-friendly RHCSA workflow. You backed up a configuration file, created a new multi-line text file with a here-document, edited key-value settings with sed -i, created a project shell environment file with export, loaded it with source, and used those variables to complete a workspace operation.