To write a basic shell script, follow these steps:
Open a Text Editor: You can use any text editor like
nano,vim, or a graphical editor.Start with a Shebang: The first line should specify the interpreter. For a Bash script, use:
#!/bin/bashWrite Your Commands: Below the shebang, write the commands you want the script to execute. For example:
echo "Hello, World!"Save the File: Save the file with a
.shextension, for example,myscript.sh.Make the Script Executable: Run the following command in the terminal to make your script executable:
chmod +x myscript.shRun the Script: Execute the script by typing:
./myscript.sh
Example of a Basic Shell Script
Here’s a simple example that lists files in the current directory and prints a message:
#!/bin/bash
# Print a message
echo "Listing files in the current directory:"
# List files
for file in *; do
echo "$file"
done
This script will print a message and then list all files in the current directory when executed.
