Script with Other Commands

Learn how to use a Bash script with other commands.

Integrating backup script into Bash

At the moment, we can run our backup script by its absolute or relative path. If we integrate it into Bash, we can call it by name. This is a convenient option when we use the script in pipelines or logical operators.

These are three ways to integrate a script into Bash:

  1. Add the script’s path to the PATH variable. Edit the ~/.bash_profile file for that.

  2. Define the alias with an absolute path to the script. Do that in the ~/.bashrc file.

  3. Copy the script to the /usr/local/bin directory and make it executable:

    sudo cp -r make-backup.sh /usr/local/bin/
    sudo chmod +x /usr/local/bin/make-backup.sh
    

The PATH variable contains this path by default. If there is no such directory in your environment, create it.

If we need to remove a declared alias, we call the unalias Bash built-in. For example, this call removes the make-backup.sh alias:

unalias make-backup.sh

Let’s suppose that we integrated the backup script with Bash in one of three ways. Then, we can launch it by name, like this:

make-backup.sh photo

Click on the Run button and run the commands discussed in this lesson.

Use student-password as password if prompted to enter a password.

Get hands-on with 1200+ tech skills courses.