Here are the steps to create and execute the bash script
- Prints the current date and time
- Creates a new directory named “test”
- Changes in the “test” directory
- Creates a new file named “test.txt” and writes the text “Hello, World!” to the file
- Prints the contents of the “test.txt” file
- Changes back to the parent directory
- 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.
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:
Current date and time: Tue Jan 6 20:23:45 UTC 2023 Hello, World!
Creating and Running bash and zsh Scripts
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. This guide 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.
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]”.
Contact Us