Quick-Start Bash Scripting Guide for Linux Newbies: Automate Your Workflow with Basic Shell Commands
Welcome to your quick-start guide on Bash scripting for Linux newbies! Ever wondered how to make your Linux experience more efficient? Bash scripting is your answer. Why Bash Scripting?
Think of Bash scripting as your personal Linux assistant. It allows you to automate repetitive tasks, manage files, and more. It's a skill that makes your Linux life easier and more efficient.
The Terminal
The terminal is your gateway to Bash scripting. It's where you'll write and run your scripts. To open the terminal on most Linux systems you can use this keyboard shortcut: Ctrl+Alt+T
Ready to create your first Bash script? If it's not already open, launch your terminal application, and let's get started!
Navigate to a Directory:
Use the cd
command to navigate to the directory where you want to create your script. To change to your home directory, type:
cd ~
Create a New File
Use the touch
command to create a new script file. Give it a .sh
extension.
touch my_first_script.sh
Edit the File
Open the new file in a text editor like nano
or vim
. Here we'll use nano
:
nano my_first_script.sh
Write the Script
Enter the following text in your my_first_script.sh
file:
echo, "Hello, world!"
Save and exit the file in nano
by pressing CTRL+X
to close, y
to confirm the changes, and then Enter
to save.
Make your script executable with the following command:
chmod +x my_first_script.sh
Run the Script
bash my_first_script.sh
You should see "Hello, world!" displayed in the terminal. Congrats, you've just created and run your first Bash script!
There's more to learn about bash scripting, but let's skip ahead. Feel free to steal the bash script below to automate note taking on your computer.
Automate Note-Taking: An Advanced Bash Script
We'll call our new script note_taker.sh
:
touch note_taker.sh
Open the script:
nano note_taker.sh
Add Shebang
#!/bin/bash
Initialize Variables:
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
note_dir=~/Notes
Create Note Directory:
if [ ! -d "$note_dir" ]; then
mkdir "$note_dir"
fi
Take Notes:
read -p "Enter your note: " note_content
echo "$note_content" > "$note_dir/note_$timestamp.txt"
Save and Exit:
chmod +x note_taker.sh
With this script, you'll be prompted to enter a note. Your note is then saved to a text file within a Notes
directory in your home folder, and it's stamped with the current date and time.
You can also add this script to your system's PATH for easier access, or set it up as an alias in your .bashrc
file for quick note-taking.
Congratulations!
You have just taken your first steps into the world of Bash script automation on Linux. Keep experimenting, and keep scripting. You'll be a Linux pro in no time.
Additional Resources
Take your bash scripting to the next level with the resources below!
- GNU Bash Manual
- Reddit's r/bash
- "The Linux Command Line" by William Shotts
- The Linux Experiment
- ExplainShell