Master Bash: Essential Shell Commands, Variables, and Scripting Basics
This guide introduces Bash shell fundamentals, covering script shebangs, execution methods, echo and printf usage, color output, variable handling, PATH manipulation, arithmetic operations, and conditional expressions, providing practical examples for Linux command‑line users.
Shell Overview
The series uses Bash, the default shell on most GNU/Linux systems, so all examples assume a Bash environment.
Shebang and Script Execution
Shell scripts typically start with a shebang line such as #!/bin/bash to specify the interpreter.
The kernel reads this line and runs the indicated interpreter.
Scripts can be run by passing them as an argument to bash script.sh or by making them executable with chmod a+x script.sh and invoking them via a relative or absolute path.
Echo Command
Key points for echo:
By default it appends a newline.
Without quotes, semicolons act as command delimiters.
Double quotes require escaping special characters; single quotes do not.
Variable substitution does not occur inside single quotes.
Common options: -n suppresses the trailing newline. -e enables interpretation of backslash‑escaped characters.
Color and text attributes can be produced with escape sequences, e.g. echo -e "\e[1;31mRed text\e[0m".
Printf Command
printfdoes not add a newline automatically; format specifiers such as %s, %d, %f are supported. Alignment can be controlled with - for left‑justified output and width specifications, e.g. printf "%-5s %-10s %-4.2f\n" 1 escape 100.123.
Variables and Environment Variables
In Bash every variable stores a string. Environment variables are used by the shell and the OS to hold special values.
Define a variable: var=value Reference it: echo $var or echo ${var} PATH is typically set in /etc/environment, /etc/profile or ~/.bashrc using export PATH="$PATH:/home/escape/bin".
Useful tricks:
String length: length=$(#var) Identify current shell: echo $SHELL or echo $0 Check for root:
[ $UID -ne 0 ]Arithmetic
Basic arithmetic can be performed with let, $(( )), or $[ ]. Advanced calculations use expr or bc, e.g. echo "scale=2; 3/8" | bc for decimal division.
Conditional Tests
Logical operators: ! – NOT -a – AND -o – OR
File tests (e.g., [-e file] checks existence, [-d file] checks for a directory, [-f file] for a regular file, etc.) and string/number comparisons ( [-n string], [ $a -eq $b ], [ $a -gt $b ]) are demonstrated.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
