Technical skill

Bash syntax: Features

Bash is a program that reads commands for a computer. It can run simple commands in order, send one command's output to another, and repeat a group of steps. These features let a short command line do routine work, such as finding a word in a file and counting its matches. In mkdir reports && cd reports, the second command runs only if the first succeeds. The symbols between commands affect what Bash runs and when.

A semicolon or newline separates commands. && runs the following command when the previous one succeeds; || runs it when the previous one fails. Parentheses run a group in a subshell, while braces group commands in the current shell when used in the proper form. That distinction affects whether changes to variables or directories remain afterward.

Pipelines pass standard output from one command to the next. For example, grep -i 'warning' app.log | sort | uniq -c filters matching lines, orders them, and counts adjacent duplicates. By default, a pipeline's status comes from its last command. Bash's set -o pipefail makes it return a failure status when a command within the pipeline fails. Redirections such as > and 2> send output or diagnostics to another destination; quote file paths with spaces.

Control structures express decisions and repetition. Bash provides if, case, for, while, and until. This loop searches matching log files and skips the literal pattern if no file matches:

for file in *.log; do
  [[ -f "$file" ]] || continue
  if grep -q 'ERROR' "$file"; then
    printf 'found in %s\n' "$file"
  fi
done

The file test prevents the loop from treating *.log as an actual filename when there are no matches under the default shell settings. A function gives a repeated command group a name:

show_size() {
  wc -c < "$1"
}

Calling show_size "notes draft.txt" passes the path as one argument. Quote function parameters when they may contain spaces or wildcard characters. Arrays can hold multiple arguments without flattening them into a string, but Bash arrays are not POSIX shell syntax.

At an interactive prompt, job control can suspend and resume foreground jobs, show them with jobs, and start a command in the background with &. In a typical terminal, Ctrl-Z suspends the foreground process group; fg resumes it in the foreground and bg in the background. Those controls manage shell jobs, not services or persistent scheduled work.

Bash also provides history, aliases, command-line editing, and programmable completion. These interactive conveniences and startup files do not automatically apply to noninteractive scripts. [[ ... ]], (( ... )), arrays, and process substitution are Bash features. A script with #!/bin/sh should use the syntax of the system's POSIX shell; choose #!/usr/bin/env bash when relying on Bash constructs. Keep eval out of routine data handling because it asks the shell to parse text again.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful