Open In App

Creating and Running bash and zsh Scripts

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating and running scripts can be a powerful tool for automating tasks and streamlining workflow. Bash and Zsh are two popular shells that can be used to create and run scripts on Linux and macOS systems. Bash, which stands for Bourne-Again Shell, is the default shell on most Linux distributions and macOS. Zsh, or Z shell, is a more feature-rich alternative to Bash that offers improved tab completion and command history among other features.

Here, we will discuss how to create and run scripts using both Bash and Zsh, including basic syntax and examples of common tasks that can be automated with scripts.

Why Use Scripting in Bash and Zsh?

Scripts allow you to execute a series of commands automatically, reducing the need for manual input and minimizing errors. By learning to script in Bash or Zsh, you can automate system maintenance tasks, file management, backups, and other repetitive tasks that would otherwise take up valuable time. Both shells support a wide range of scripting capabilities, including loops, conditional statements, and variables, enabling the automation of complex workflows.

How to Create and Run Scripts in Bash and Zsh

To create a script, you will first need to open a text editor and type in the commands you want the script to execute. It is important to make sure that the script is saved with the '.sh' extension for Bash or '.zsh' extension for Zsh. Once your script is saved, you will need to make it executable by running the command "chmod +x [scriptname]" in the terminal.

To run the script, you can use the terminal command "./[scriptname]". If your script is located in a directory that is not in your system's PATH, you will need to specify the full path to the script when running it. For example, if your script is located in the Documents folder, you would run "./Documents/[scriptname]".

Creating a Bash Script

  1. Prints the current date and time
  2. Creates a new directory named "test"
  3. Changes in the "test" directory
  4. Creates a new file named "test.txt" and writes the text "Hello, World!" to the file
  5. Prints the contents of the "test.txt" file
  6. Changes back to the parent directory
  7. Deletes the "test" directory

The script contains the explanation of each step with the command explanation.

Step 1. Open a terminal window.
Step 2. Use the nano or vi commands to open a new blank file in a text editor. For example nano myscript.sh.

$ nano myscript.sh

Step 3. Type the bash commands that you want to include in the script. you can see we have written a sample script below.

#!/bin/bash

# This line prints the current date and time
echo "Current date and time: $(date)"

# This line creates a new directory named "test"
mkdir test

# This line changes into the "test" directory
cd test

# This line creates a new file named "test.txt" and writes the text "Hello, World!" to the file
echo "Hello, World!" > test.txt

# This line prints the contents of the "test.txt" file
cat test.txt

# This line changes back to the parent directory
# The 'cd ..' command is used to change to the parent directory (i.e., the directory one level up)
cd ..

# This line deletes the "test" directory
rm -r test

Step 4. Save the file by pressing CTRL + X, then Y, and then Enter.

Save and Exit the Text Editor
Save and Exit the Text Editor

Step 5. Make the script executable by running the chmod command: 'chmod +x myscript.sh'

$ chmod +x myscript.sh

Step 6. Run the script:

./myscript.sh

And here is an example output of the script:

output of the script
output of the script
Current date and time: Tue Jan 6 20:23:45 UTC 2023
Hello, World!

Creating a Zsh Script

Step 1: Open a terminal window.
Step 2: Use the nano or vi commands to open a new blank file in a text editor. For example: nano myscript.zsh
Step 3: Type the zsh commands that you want to include in the script.

#!/bin/zsh

# This line sets the variable 'filename' to the first argument passed to the script
filename=$1

# This line prints the hostname of the current system
echo "Hostname: $(hostname)"

# This line creates a subshell and runs the 'ls' command inside it.
# The output of the 'ls' command is then stored in the 'ls_output' variable
ls_output=$(ls)

echo "Output of 'ls' command: $ls_output"

# This line checks if the file specified by the 'filename' variable exists
# and is a regular file (not a directory). If the file exists, it prints the contents
# of the file using the 'cat' command. If the file does not exist, it prints an error message
if [[ -f "$filename" ]]; then

  cat "$filename"

else

  echo "Error: File not found"

fi

# This line prints the current working directory
echo "Current working directory: $(pwd)"

# This line prints the current user's home directory
echo "Home directory: $HOME"

# This line reads input from the user and stores it in the 'input' variable
echo -n "Enter your name: "

read input

# This line prints the value of the 'input' variable
echo "Your name is: $input"

Step 4: Save the file by pressing 'CTRL + X', then 'Y', and then Enter.

 


Step 5: Make the script executable by running the chmod command: chmod +x myscript.zsh

$ chmod +x myscript.zsh

Step 6: Run the script by typing its name at the command prompt, followed by any arguments that the script expects: ./myscript.zsh argument1 argument2

./myscript.zsh argument1 argument2

Here is the output of the above zsh script after executing it:

Hostname: myhostname
Output of 'ls' command: test.txt
Hello, World!
Current working directory: /home/user/documents
Home directory: /home/user
Enter your name: John
Your name is: John

Conclusion

Bash and Zsh scripts are powerful tools for automating tasks and streamlining workflow on Linux and macOS systems. Creating scripts is as simple as writing a list of commands in a text editor and saving it with the appropriate file extension. Running the script is as easy as using the command "./[scriptname]" in the terminal.

Both Bash and Zsh offer a variety of features and capabilities, including control structures, loops, and conditional statements, which can be used to automate more complex tasks. With a little bit of practice and experimentation, anyone can learn to create powerful scripts that can save time and effort on a daily basis.


Next Article

Similar Reads