How to run shell script in Linux || shell script example with basic commands

How to run shell script in Linux || shell script example with basic commands

Below are the steps to run a shell script in Linux

  1. Open a terminal: Launch the terminal application on your Linux system. You can usually find it in the applications menu or by searching for "terminal" or "console."

  2. Navigate to the directory: Change your working directory to the location where your shell script is located. Use the cd command followed by the path to the directory. For example, if your script is in the "Documents" folder, you can use the command: cd ~/Documents

  3. Make the script executable: If the script file doesn't have executable permissions, you need to give it executable rights using the chmod command. For example, to make a script named "myscript.sh" executable, run: chmod +x myscript.sh

  4. Run the script: Execute the shell script by typing its name preceded by ./. For example, if your script is named "myscript.sh," run: ./myscript.sh

Here's an example of a simple shell script with basic commands:

#!/bin/bash

# This is a simple shell script example

# Display a welcome message
echo "Welcome to the script!"

# List the files in the current directory
ls

# Create a new directory
mkdir mydir

# Change to the new directory
cd mydir

# Display the current working directory
pwd

# Print a message
echo "Script execution complete!"

Save the script into a file with a .sh extension (e.g., myscript.sh), and follow the steps mentioned above to run it.