To run a bash script, you typically make it executable with chmod +x script.sh and then run it with ./script.sh, or you can invoke it directly with bash script.sh. This article explains both methods, when to use each, and common issues you might encounter.
What Is a Bash Script?
A bash script is a plain text file containing a series of commands that the Bash shell (the default command interpreter on most Linux and macOS systems) executes sequentially. Scripts are used to automate repetitive tasks, combine multiple commands, and create custom tools. They typically start with a shebang line (#!/bin/bash) that tells the system which interpreter to use, followed by the commands you want to run.
Prerequisites: Creating a Script File
Before you can run a script, you need to create one. Use any text editor (like nano, vim, or a GUI editor) to create a file, for example myscript.sh. The .sh extension is conventional but not required—the file just needs to contain valid bash commands and be readable by the user. A minimal example:
#!/bin/bash
echo "Hello, world!"
Make sure the file is saved with Unix line endings (LF), not Windows CRLF, otherwise you may get errors like command not found.
Method 1: Running with bash Command
The simplest and most portable way is to run the script with the bash command:
bash myscript.sh
This works regardless of whether the file is executable, and it doesn’t require the shebang line to be correct (though it’s still good practice to have it). It also lets you pass arguments: bash myscript.sh arg1 arg2. Inside the script, these are available as $1, $2, etc.
Method 2: Making the Script Executable
To run a script like a standalone program (e.g., ./myscript.sh), you need to set the execute permission:
- Open a terminal in the directory containing the script.
- Run
chmod +x myscript.shto add execute permission. - Run
./myscript.shto execute it.
The ./ prefix is necessary because the current directory is usually not in the PATH. If you omit it, the shell will search the PATH and likely not find your script. You can also add the directory to your PATH, but that’s rarely needed for simple tasks.
Which Method Should You Choose?
- Use
bash script.shwhen you want to run a script temporarily, when the file isn’t executable, or when you want to override the shebang (e.g.,bash -x script.shfor debugging). - Use
./script.shwhen you want the script to behave like a real command, especially if it’s part of a larger project or you need to run it often. - For scripts without a shebang,
bash script.shis the only way to run them directly.
Common Problems and How to Fix Them
Several issues can prevent a script from running. Here are the most frequent ones:
- Permission denied: You forgot to
chmod +x. Fix: runchmod +x myscript.sh. - Command not found: The script has Windows line endings or the shebang points to a wrong path. Fix: convert line endings (
sed -i 's/\r$//' myscript.sh) or correct the shebang. - Bad interpreter: The shebang path is wrong (e.g.,
#!/bin/bashbut Bash is installed elsewhere). Fix: use#!/usr/bin/env bashto locate Bash automatically. - Script runs but does nothing: The script may not have execute permission, or the current directory is not in PATH. Try
./myscript.shexplicitly.
Safety and Best Practices
Always review a script before running it, especially if you downloaded it from the internet. Running bash script.sh executes every command in it with your user privileges, so a malicious script could delete files or send data. Use bash -n script.sh to check syntax without executing, and bash -x script.sh to trace execution step by step. Also, quote variables to avoid word splitting, and use set -e to exit on errors if you want the script to stop at the first failure.
Frequently Asked Questions
What does the shebang (#!) do?
The shebang tells the operating system which interpreter to use when the script is executed directly (e.g., ./script.sh). For a bash script, it’s usually #!/bin/bash. If you run the script with bash script.sh, the shebang is ignored.
Can I run a bash script on Windows?
Yes, you can install Windows Subsystem for Linux (WSL) and run bash scripts from the WSL terminal. Alternatively, you can use Git Bash or Cygwin, but they may have slight differences in behavior.
How do I pass arguments to a script?
After the script name, add arguments separated by spaces: ./myscript.sh arg1 arg2. Inside the script, $1 is the first argument, $2 the second, and so on. $@ contains all arguments.
Why do I get “./myscript.sh: Permission denied”?
This means the file does not have execute permission. Run chmod +x myscript.sh to add it, then try again. If you still get the error, check the file system mount options (e.g., on a USB drive mounted with noexec).
What is the difference between bash script.sh and ./script.sh?
When you use bash script.sh, you are explicitly invoking the Bash interpreter to read and execute the file. When you use ./script.sh, the system reads the shebang line to determine the interpreter. The latter requires execute permission, but the former does not.
Can I run a script without making it executable?
Yes, by using bash script.sh or sh script.sh (if the script is POSIX-compatible). This is a common workaround when you cannot change file permissions.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It