2.5 C
New York
Friday, December 8, 2023
HomeTechnologyBash Script Cheat Sheet | The Ultimate Bash Scripting Cheat Sheet for...

Bash Script Cheat Sheet | The Ultimate Bash Scripting Cheat Sheet for Beginners

Bash Script Cheat Sheet: Bash scripting is a powerful way to automate tasks and customize your Linux environment. With just a few lines of code, you can save yourself hours of repetitive work.

This bash scripting cheat sheet will give beginners a quick reference guide to get started. It covers the most common commands, variables, and functions you’ll need. Follow along and you’ll be writing basic scripts in no time!

Getting Started with Bash Scripting

Bash scripts are plaintext files with a .sh extension. You can create and edit them in any text editor.

To run a script, you first need to make it executable with the chmod command:

chmod +x myscript.sh

Then execute it by typing the path to the script:

./myscript.sh

That’s the basics! Now let’s look at some key commands and syntax.

Commands

  • echo – Print text or variables
  • Example: echo "Hello World"
  • # – Comment out a line
  • #!/bin/bash – Shebang declaring Bash as the script interpreter
  • source – Import functions from another script
  • sleep – Delay for a number of seconds
  • date – Print or set the system date and time
  • if – Execute code based on condition
  • fi – End an if statement
  • for – Execute code in a loop
  • done – End a for loop

Variables

  • VAR="value" – Declare a variable
  • $VAR – Print the value stored in a variable
  • ${VAR} – Use value of VAR variable
  • $1 – Access first argument passed to script
  • $0 – Script name
  • $# – Number of arguments passed
  • $@ – All arguments passed
  • $? – Return value of last executed command

Functions

function_name() {

  # code

}
  • Declare functions this way
  • Call them by typing function_name
  • Use return to exit function with value
  • $1, $2 etc work as function arguments

Conditional Statements

if [ condition ]; then

  # code

elif [ condition ]; then

  # code

else

  # code

fi
  • Compare strings and integers using -eq, -ne, -gt, -lt etc.
  • Use && and || for AND/OR logical operations
  • Test conditions with:
  • [ -z STRING ] – Empty string
  • [ -n STRING ] – Not empty string
  • [ INT1 -eq INT2 ] – Equal
  • [ INT1 -ne INT2 ] – Not equal
  • [ INT1 -lt INT2 ] – Less than
  • [ INT1 -le INT2 ] – Less than or equal
  • [ INT1 -gt INT2 ] – Greater than
  • [ INT1 -ge INT2 ] – Greater than or equal

Loops

for i in {1..10}; do

  # code

done
  • for loops iterate over list of items
  • Use seq to generate numeric sequences
  • Loop through output of a command like ls
while [ condition ]; do

  # code

done
  • while loops execute until condition is false
  • Infinite loop with while true; do #code done

Putting It All Together

Once you know the basics, you can start combining commands, variables, conditions and loops to create simple yet powerful scripts.

Here is an example script that backs up a directory:

#!/bin/bash

SOURCE="/home/user/documents"
TARGET="/backup"
DATETIME=$(date +%Y_%m_%d_%H_%M)
ARCHIVE="$TARGET/backup_$DATETIME.tar.gz"

echo "Backing up $SOURCE to $TARGET"

tar -czf $ARCHIVE $SOURCE

echo "Backup created at $ARCHIVE"

The key is to break down your task into discrete steps. Then use Bash’s features at your disposal to automate each part.

Conclusion

Bash scripting allows you to unlock the power of Linux for customizing your environment. This cheat sheet summarizes the essential syntax, operators and features.

Keep it handy as you learn scripting basics. Refer to it when creating your first scripts. It provides a quick reference for variables, loops, conditional logic and more.

With some practice, you’ll soon be automating routine tasks with ease. So what are you waiting for? Start scripting!

Also Read: Apple AirTag: The Ultimate Tracking Device

RELATED ARTICLES

Leave a Reply

- Advertisment -

Most Popular

Recent Comments

%d