WCC logo

CIS120Linux Fundementals

Creating a Bash Script, Comments, Variables and Read

A Bash script is a file containing a series of commands that are executed by the Bash shell, a popular command-line interpreter in Unix-like operating systems. These scripts are used to automate tasks, manage system operations, and perform complex sequences of commands with a single execution.

Writing a Simple Bash Script

Creating a Bash script involves writing commands in a plain text file and then executing the file. Here's an example of a simple Bash script that prints "Hello, World!" to the console:

  1. Open a text editor and write the following lines:

    #!/bin/bash
    # This is a comment
    echo "Hello, World!"
    
    • The #!/bin/bash at the top is called a shebang. It tells the system to use the Bash interpreter to execute the script.
    • The echo command prints text to the terminal.
    • The line starting with # is a comment, which is ignored by the shell and used to add explanations or notes.
  2. Save the file with a .sh extension, for example, hello_world.sh.

Changing Script Permissions

Before running the script, you need to change its permissions to make it executable. This can be done using the chmod command:

chmod 700 hello_world.sh

The 700 permission means that the file's owner has read, write, and execute permissions, while no one else has any permissions. This ensures that only the creator can run the script.

Running a Bash Script

To run the script, navigate to the directory where the script is saved and use one of the following methods:

  1. Using ./:

    ./hello_world.sh
    

    The ./ indicates that the script is located in the current directory.

  2. Using bash:

    bash hello_world.sh
    

    This explicitly uses the Bash interpreter to run the script.

  3. Using the full path:

    /path/to/your/script/hello_world.sh
    

    This method runs the script by specifying its full path.

Comments in Bash Scripts

Comments in Bash scripts are lines that begin with #. They are not executed by the shell and are used to add descriptions, explanations, or notes within the script. Comments are useful for:

For example:

#!/bin/bash
# This script prints a greeting message
echo "Hello, World!"  # This line prints "Hello, World!"

Variables in Bash Scripts

Variables in Bash scripts are used to store data that can be referenced and manipulated throughout the script. Variables make scripts more flexible and easier to manage. You can assign a value to a variable using the = operator and access its value by prefixing the variable name with a $ sign. The $ sign is necessary to differentiate the variable's value from the variable's name.

IMPORTANT NOTE: Variable names need to be repesentative of the data. For example, you would using the variable firstName to contain someones first name. Also variables in bash scripting (unless you use other libraries) contain strings which is any text within double quotes, like firstName="Scott". Or numbers which is just a whole number like num=43. Finally, variable names cannot contain spaces. For example, the variable first name must be written as first_name or using camel case like firstName where the first letter of each word is capitalized.

For example:

#!/bin/bash
# This script uses a variable to print a message
greeting="Hello, World!"
echo $greeting

In this script:

Without the $ sign, the shell would interpret greeting as a literal string rather than the variable's value.

Using read Command

The read command in Linux is a simple and powerful way to get input from a user in a script. When you use read, the command waits for the user to type something and press Enter, and then it stores that input in a variable you can use in the rest of your script. This is especially useful in interactive scripts where you need the user’s input to make decisions or perform actions.

Let’s look at a basic example of how read works. Suppose you want to ask the user for their name and then greet them:

echo "What is your name?"
    read name
    echo "Hello, $name!"
    

In this script:

  1. echo "What is your name?" displays the question to the user.
  2. read name waits for the user to type their name and press Enter. The input is stored in a variable called name.
  3. echo "Hello, $name!" prints a greeting that includes the user’s name.

When the script runs, the user will see “What is your name?” on the screen. They can type their name, and the script will respond with “Hello, [name]!” where [name] is whatever they typed.

The read command can also handle multiple pieces of input. For example, if you want to ask for both a first and last name, you can do this:

echo "Enter your first and last name:"
    read first last
    echo "Hello, $first $last!"
    

Here, read first last allows the user to type both their first and last name separated by a space. The first word they type goes into the first variable, and the second word goes into the last variable.

You can also use options with read to make it more useful. For example, the -p option lets you display a prompt on the same line as the read command, and the -s option hides the user’s input, which is helpful for passwords:

read -p "Enter your username: " username
    read -sp "Enter your password: " password
    echo
    echo "Username: $username, Password: [hidden]"
    

In this example:

  1. -p "Enter your username: " displays the prompt on the same line.
  2. -s hides the user’s input for password.
  3. The echo command after the read for password is there to move to a new line after the hidden input.

The read command makes scripts interactive and lets users provide the information you need, allowing for dynamic and responsive scripts. It’s an essential command for adding interactivity to your Linux scripts!

Understanding /bin and Adding to PATH

The /bin directory (short for "binary") is one of the standard directories in Unix-like systems where essential command binaries are stored. You should have a bin directory in your home directory, if not, you can create one. If you put your scripts in the bin directory you can call them but just entering the script by name (no need for ./ or bash or a full path). To run scripts or commands from any directory you can add their directory to the system's PATH environment variable. This allows the system to locate the executable files in the specified directories.

To add a directory to your PATH, you can modify your shell configuration file, such as .bashrc or .bash_profile. Here’s how you can add a custom directory (e.g., ~/scripts) to your PATH:

  1. Open your shell configuration file in a text editor:

    vim ~/.bashrc
    
  2. Add the following line at the end of the file:

    export PATH="$PATH:~/scripts"
    
  3. Save the file and reload the configuration:

    source ~/.bashrc
    

Now, any executable files in the ~/scripts directory can be run from any location in the terminal.

Summary

A Bash script is a powerful tool for automating tasks in Unix-like operating systems. Writing a script involves creating a text file with a series of commands, changing its permissions to make it executable, and using comments to document the script. Variables in Bash scripts allow you to store and manipulate data, making your scripts more flexible and maintainable. To run a script, you can use the ./ notation, the bash command, or the full path. Adding directories to the PATH environment variable enables you to run scripts and commands from any location without specifying their full path. By understanding these fundamental concepts, you can create efficient and effective Bash scripts to streamline your workflow.