Bash Scripts

A Bash Script is simply a text file containing a series of commands. When you run the script, the shell executes those commands one by one, allowing you to automate repetitive tasks.

1. The Shebang (#!)

The first line of any Bash script must be the Shebang. This tells the operating system which interpreter should be used to run the file.

Bash

#!/bin/bash

2. Creating Your First Script

Let's create a simple script called hello.sh.

Bash

#!/bin/bash
# This is a comment
echo "Hello, world!"
echo "Today's date is: $(date)"

3. Making it Executable

By default, new files do not have the execute permission. You must add it using chmod.

Bash

chmod +x hello.sh

4. Running the Script

To run a script in your current directory, you must use ./ before the filename.

Bash

./hello.sh

Summary: 1. Write the script with a #!/bin/bash header.
2. Save the file (e.g., myscript.sh).
3. Make it executable: chmod +x myscript.sh.
4. Run it: ./myscript.sh.