0
0
Bash Scriptingscripting~5 mins

Creating a script file (.sh) in Bash Scripting

Choose your learning style9 modes available
Introduction
A script file (.sh) lets you save commands to run them easily anytime without typing again.
You want to automate daily tasks like backups.
You need to run a set of commands repeatedly.
You want to share a set of commands with a friend or colleague.
You want to organize commands for a project in one place.
You want to run commands on a remote server automatically.
Syntax
Bash Scripting
#!/bin/bash

# Your commands go here
The first line #!/bin/bash tells the system to use the bash shell to run the script.
Make sure to give your script file execute permission with chmod +x filename.sh.
Examples
A simple script that prints Hello, world! to the screen.
Bash Scripting
#!/bin/bash

echo "Hello, world!"
A script that lists files in the /home/user directory.
Bash Scripting
#!/bin/bash

ls -l /home/user
A script that shows the current date and time.
Bash Scripting
#!/bin/bash

# This script shows the current date and time
date
Sample Program
This script prints a welcome message when run.
Bash Scripting
#!/bin/bash

# This script greets the user

echo "Welcome to your script!"
OutputSuccess
Important Notes
Always start your script with #!/bin/bash to ensure it runs with bash.
Use chmod +x yourscript.sh to make the script executable.
Run your script by typing ./yourscript.sh in the terminal.
Summary
A .sh file saves commands so you can run them anytime.
Start with #!/bin/bash to tell the system how to run it.
Make the file executable with chmod +x before running.