Hello! Ready to embark on a thrilling journey through the marvelous world of Bash? Let's dive into the ocean of shell scripting with gusto and unveil its intriguing secrets.
Before we start exploring Bash's features and capabilities, it's essential to understand its roots. Bash, short for "Bourne-Again SHell," was created in 1989 by Brian Fox as a free software alternative to the Bourne Shell (sh), which was developed by Steve Bourne in 1979.
Brian Fox, one of the founding members of the Free Software Foundation, combined the features of the Bourne Shell with additional elements from C Shell (csh) and Korn Shell (ksh). Bash was released as part of the GNU Project and has since become the default shell for various UNIX-based systems, including macOS and Linux distributions.
"When I wrote the Bourne-Again shell, I wanted to add lots of new functionality from the other shells at the time." - Brian Fox
To get acquainted with the elegance of Bash, let's start by examining some basic commands:
echo - Display text or the value of environment variables:
echo "Hello, world!"
echo "My home directory is $HOME."
pwd - Print the current working directory:
pwd
ls - List files and directories:
ls
ls -l # Long format
cd - Change directory:
cd /path/to/directory
cd .. # Go up one level
cd - # Go back to the previous directory
cat - Concatenate and display file(s):
cat filename.txt
cat file1.txt file2.txt > combined.txt
grep - Search for patterns in text:
grep "pattern" filename.txt
grep -r "pattern" /path/to/directory
find - Locate files and directories:
find /path/to/directory -name "filename"
find /path/to/directory -type d # Find directories only
| (pipe) - Connect the output of one command to the input of another:
cat filename.txt | grep "pattern"
> (redirection) - Redirect the output of a command to a file:
echo "Hello, world!" > output.txt
ls -l > directory_listing.txt
Now that you have a grasp of the basic commands, let's explore the fascinating realm of Bash scripting!
Bash provides support for variables, allowing you to store values and reuse them later:
name="Ada Lovelace"
age=36
echo "My name is $name, and I am $age."
Unveil the power of decision-making and loops with Bash's control structures!
Evaluate conditions and execute code accordingly:
number=42
if [ $number -eq 42 ]; then
echo "The answer to life, the universe, and everything."
elif [ $number -lt 42 ]; then
echo "Too low."
else
echo "Too high."
fi
Iterate over a list of items or a range of values:
for name in Alice Bob Carol; do
echo "Hello, $name!"
done
for ((i=1; i<=5; i++)); do
echo "Iteration $i"
done
Execute code while a condition remains true:
counter=3
while [ $counter -gt 0 ]; do
echo "Counter: $counter"
counter=$((counter - 1))
done
Create reusable blocks of code with Bash functions:
function greet() {
local name="$1"
echo "Hello, $name!"
}
greet "Ada"
greet "Charles"
Interact with the system by reading environment variables and running external programs:
echo "My home directory is $HOME."
echo "My user is $(whoami)."
Unleash the full potential of Bash with these handy tips and tricks:
Command substitution - Insert the output of a command into another command or a variable:
files=$(ls -1 | wc -l)
echo "There are $files files in the current directory."
Process substitution - Use the output of a command as a file-like input for another command:
diff <(command1) <(command2)
Aliases - Create shortcuts for frequently used commands:
alias ll="ls -l"
ll
History expansion - Recall and modify previous commands:
!! # Repeat the last command
!-2 # Repeat the command before the last
^foo^bar # Replace the first occurrence of 'foo' with 'bar' in the last command and execute it
Brace expansion - Generate multiple strings or sequences with a compact notation:
echo file{1..3}.txt # Outputs: file1.txt file2.txt file3.txt
Here documents and here strings - Redirect a multiline string or a single string to a command's input:
cat << EOF
This is a here document.
It spans multiple lines.
EOF
tr '[:lower:]' '[:upper:]' <<< "here string"
Congratulations, you've unearthed a treasure trove of Bash knowledge! The vibrant and versatile world of Bash is now at your fingertips. Use this newfound power to automate tasks, solve problems, and have fun exploring the depths of shell scripting. Remember, the only limit is your imagination!
Happy scripting, and may the power of Bash be with you!
Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.