Setting Up a Scripting Environment

When first starting learning Linux, I didn’t realize lot of it lies beneath the surface. Linux still holds on to it’s developmental roots and a good deal of it’s power can be found directly from the command line. Windows doesn’t have this type of functionality, and though Mac OS X has some of it few people know about it. If needing to do powerful or automated commands with Linux (whether it be switch mouse buttons, or launch multiple programs at once), many times I can turn to the command line and write a bash script for it. The command line can be very powerful: there are few things that can only be done only from a window, and many more from the command line that can’t be done in a window.

Setting up a scripting environment means creating a place to store the scripts, easily getting to them, and executing them like a regular command.

Directory Setup

First thing I do is set up a directory to place the scripts in. This directory is usually best in the home folder and is preferably invisible as it’s not necessary to see it all the time. This may sound inconvenient at first but since commands will be run from the terminal it is quickly gotten used to. I like to name the directory ~/.scripts, others follow Linux filesystem convention and use ~/.local/bin (dot files are hidden files and are not shown unless explicitly stated):

mkdir ~/.scripts

The tilda character (~) signifies that the directory is the home directory and is used as a shortcut because it is quicker than typing /home/user. To quickly switch to that directory, I create a shortcut in the bash configuration file. Shortcuts can be defined in the bash configuration file using aliases. The bash configuration file is called ~/.bashrc. Adding the shortcut:

alias cds="cd ~/.scripts"

cds tells me to: change to the directory of scripts. After I save it, I re-source the bash configuration file to reload the new settings.

source ~/.bashrc

Now typing the shortcut cds will change to the script directory.

Run Scripts Just Like Regular Commands

I create new scripts here or put those I find here. Creating a script is outside this post but once they are here they will need to be executable:

chmod +x script-name

To be able to run the script like a regular command, the bash shell will need to be let known of the new executable path (~/.scripts). Anytime a command is run in bash, it looks for programs or scripts that are in the path directive. Currently known paths can be discovered by:

echo $PATH

To add the script directory to the known paths, it needs to be defined in the ~/.bashrc file. The bash configuration file may already have some paths defined in the export PATH... line. If it does, the script directory can be added to the line. If it doesn’t, I add both the script directory and the current paths ($PATH) to be sure the new path(s) don’t override the old:

export PATH="~/.scripts:$PATH"

Different paths are separated by a colon (:) and as many can be added as needed. Saving and sourcing ~/.bashrc will have the new directory(ies) be recognized by the bash shell.

Related

  • If you like to learn more about copying scripts (or text) from a window and pasting it to a file from the command line, see Command Line to Clipboard.

3 thoughts on “Setting Up a Scripting Environment

  1. Wesley

    So anything in my home directory is in my path , correct ?
    This is sorta what I’ve been looking for , an explanation , ‘tute along this line .
    Thanks ! I’m aware the resources are out there – sometimes you just have to run into them for it to make sense . I’m thick headed and still learning . Thanks again for the explanation. Sticky material

    Reply

Leave a comment