This guide covers advanced topics in Bash scripting, building on the foundational concepts presented earlier. Explore powerful features and techniques to enhance your Bash scripting skills.
grep
grep
for pattern matching.
# Example: Searching for lines containing "error" in a log file
grep "error" logfile.txt
sed
(Stream Editor) sed
.
# Example: Replacing "apple" with "orange" in a file
sed 's/apple/orange/g' input.txt
awk
Programming awk
for advanced text processing. awk
scripts for data manipulation.
# Example: Summing values in the second column of a CSV file
awk -F',' '{ sum += $2 } END { print sum }' data.csv
# Example: Running a command in the background
long_running_command &
# Example: Handling the SIGTERM signal
trap 'echo "Received SIGTERM"; cleanup_function' TERM
bg
, fg
, and jobs
commands.
# Example: Running a command in the background and bringing it to the foreground
command &
fg
# Example: Checking the exit code of the last command
if [ $? -eq 0 ]; then
echo "Command succeeded"
else
echo "Command failed"
fi
# Example: Gracefully handling SIGINT (Ctrl+C)
trap 'echo "Received SIGINT"; cleanup_function' INT
set -x
and set +x
# Example: Using set -x for debugging
set -x
# Your script commands here
set +x
PS4
Environment Variable
# Example: Customizing the PS4 prompt
PS4='Executing: '
Continue to Part 9: Best Practices.
Go back to Bash Scripting Guide.
Visit other Developer Guides.