Bash Tips & Tricks

Bash Tips & Tricks

Common tips and tricks to use Bash

So far we have explained the basics of Bash scripting. Before we go into advanced topics here are some useful Bash tips and tricks you can already use:

  • Use alias to create shortcuts for long commands. For example: alias ll='ls -l'

  • Use tab completion to auto-complete file and directory names. Press tab twice to see all options.

  • Use history to view and run previous commands. history lists all commands and ! runs a specific command.

  • Pipe multiple commands together to perform complex tasks.
    For example: ls | grep .py | more

  • Use wildcard characters like * and ? to match files.
    For example: ls *.py

  • Use && to run multiple commands only if the previous one succeeds and || to run if the previous fails.

  • Use $() for command substitution.
    For example: echo $(ls)

  • Redirect stdout and stderr to files using >, >> and 2>.
    For example: ls > output.txt 2> errors.txt

  • Use variables to store and reuse data.
    For example: MYVAR="some value" && echo $MYVAR

  • Enclose variables in {} when they are next to other text.
    For example: echo "My name is ${NAME}"

  • Use if, case, for, while and until loops for control flow.

  • Define functions to organize and reuse code.
    For example: function_name () { commands; }

  • Use arrays to store lists of data.
    For example: MYARRAY=(value1 value2 value3)

  • Comment your scripts using #.
    For example: # This is a comment


Bash One-Liners

Bash one-liners are short Bash commands that perform a specific task in a single line. They allow you to perform tasks quickly and efficiently from the command line.

Some examples of Bash one-liners I previously mentioned are:

  • List files by size: ls -lS This will list all files in the current directory sorted by size (largest first).

  • Count the number of lines in a file: wc -l < filename This will count the number of lines in the given filename.

  • Grep for a pattern in files: grep "pattern" * This will grep for the pattern in all files in the current directory.

  • Sort lines of a file: sort filename This will sort the lines in the given filename.

  • Delete a file: rm filename This will delete the given filename.

The benefits of Bash one-liners are:

  • Concise - They take up minimal space and are easy to remember.

  • Quick - They allow you to perform tasks quickly without going into an editor.

  • Reusable - You can save them in your Bash history for reuse.

  • Chained - You can chain multiple one-liners together to perform complex tasks.

Here are some useful Bash one-liners:

  • List files by size: ls -lS

  • List files by modification time: ls -lt

  • Count number of lines in a file: wc -l

  • Grep for a pattern in files: grep "pattern" *

  • Reverse lines in a file: tac filename

  • Sort lines of a file: sort filename

  • Concatenate files: cat file1 file2 > outputfile

  • Create an empty file: touch filename

  • Copy a file: cp filename copyname

  • Rename a file: mv oldname newname

  • Delete a file: rm filename

  • Delete multiple files: rm filename

  • Create a directory: mkdir dirname

  • Remove a directory: rmdir dirname

  • View last 10 lines of a file: tail -n 10 filename

  • View first 10 lines of a file: head -n 10 filename

  • Compress a file: gzip filename

  • Decompress a file: gunzip filename.gz

  • Find files by name: find . -name "filename"

  • Kill a process by ID: kill -9 1234

Hope these Bash one-liners help! Let me know if you have any other questions.

So in summary, Bash one-liners allow you to perform common Unix tasks quickly and efficiently from the command line using short, concise commands.


Advanced Topics

Based on the topics we have already covered, here are some advanced Bash topics that would be good to cover to complete the course:

  1. Signal Handling - Using the trap command to catch signals and handle them gracefully in Bash scripts. This helps make scripts more robust.

  2. Process Substitution - Using <() and >() to pass the output/input of commands as files. This allows commands to communicate without using temporary files.

  3. Debugging Techniques - Using set -x, bash -x, and other techniques to debug Bash scripts and find issues.

  4. Subshells - Spawning subprocesses using ( ) to run commands in isolation. This is useful for testing.

  5. Command Line Arguments - Accessing command line arguments passed to a Bash script using $1, $2, etc.

Those would be some good advanced Bash topics to cover to complete the course, building on the foundational topics we have already discussed. Please comment below to help me identify other topics you may be intrestead to learn and I have not cover yet.


Disclaim: This article is generated with aid from Rix (an advanced AI bot).