Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchA Bash script is a plain-text file containing commands for Bash to run. To make one, put #!/bin/bash on its first line, add a command, save the file, and run it with the appropriate permissions. This guide builds from a one-command script to a reusable directory-backup example, including arguments and interactive input.
Create and run your first Bash script
-
Open a plain-text editor and create a file named
hello.sh. Use a text editor rather than a word processor, which may add formatting. -
Add a shebang on the first line and a comment describing the task:
#!/bin/bash # Print a greeting echo "Hello, world!"The
#!line is called a shebang. When you execute the file directly, it tells the system to use/bin/bashas the interpreter. This path is appropriate on many Linux systems; if Bash is installed elsewhere, use its actual path.Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.#1 Best Overall
SaleLearning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))- Used Book in Good Condition
-
Save the file, then grant its owner read, write, and execute permissions:
chmod 700 hello.shWith mode
700, the owner can read, write, and execute the file; group members and other users have no permissions. Use this when the script should be private to its owner. To check the mode and file details, run:ls -lh hello.sh -
Run it from the directory containing the file:
./hello.shThe expected output is
Hello, world!. The./tells the shell to run the file in the current directory.
Put a useful command in the script
Once the basic structure is clear, replace the greeting with a small task you understand. For example, this script synchronizes one directory to another:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
#!/bin/bash
# Copy a directory to a backup location
rsync -av "$HOME/Documents/" "$HOME/backup/Documents/"
rsync copies files and directories; the -a option enables archive behavior and -v prints the files being processed. The trailing slash on the source path means to copy the contents of Documents into the destination directory. Before relying on a backup script, confirm that the source and destination paths are correct and that you have permission to write to the destination.
Use variables for paths you may change
If a path appears multiple times or is likely to change, store it once in a variable. For example:
Rank #4
#!/bin/bash
# Copy a source directory to a backup destination
SOURCEDIR="$HOME/Documents/"
DESTDIR="$HOME/backup/Documents/"
rsync -av "$SOURCEDIR" "$DESTDIR"
Now you can update a path in one place rather than searching through several commands. The quotation marks keep paths containing spaces together as one argument.
Accept a path as a command-line argument
$1 represents the first argument supplied after the script name. This example accepts a source directory and uses a fixed destination:
Best Value
#!/bin/bash
# Back up the directory supplied as the first argument
SOURCEDIR="$1"
DESTDIR="$HOME/backup/"
if [ -z "$SOURCEDIR" ]; then
echo "Usage: $0 SOURCE_DIRECTORY" >&2
exit 1
fi
rsync -av "$SOURCEDIR" "$DESTDIR"
Run it by supplying the source path:
./backup.sh "$HOME/Documents/"
Here, $0 refers to the script name, and the check prevents the command from running when no source argument is provided. Quoting the argument protects paths that contain spaces. The script exits with 1 for the missing-argument error; a successful command normally returns 0, while a failure returns a non-zero status.
Ask for input while the script runs
For an interactive script, use read to collect a value from the person running it:
#!/bin/bash
# Ask for a name and greet the user
printf "What is your name? "
read name
printf "Hello, %sn" "$name"
The script pauses at read name until the user enters input. Use a command-line argument instead when the script should run unattended or be called by another program.
Use exit statuses to report success or failure
Shell commands return an exit status: zero conventionally means success, and a non-zero value indicates failure. Administrative scripts should preserve that distinction so users and other programs can tell whether a task completed. Linux Standard Base guidance for init scripts follows this success/failure convention. For a script that runs a single command last, its exit status is typically the script’s status as well; scripts with more steps should check important commands and choose an appropriate status deliberately.
Choose what to learn next
For a next step, choose material based on whether you need a reference or guided practice, and whether it covers the Bash features you plan to use. The Linux Foundation’s Bash credential description lists simple commands, basic syntax, builtins, variables, special parameters, and environment concepts. A reference such as the Advanced Bash-Scripting Guide can suit readers ready to explore beyond a first exercise; structured training is a better fit for those who want a guided course. No learning-speed or productivity figures are established for these options.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




