Write Simple Shell Scripts in Linux
Learn to create, edit, make executable, and run basic Bash shell scripts using commands, variables, and user input.
A shell script is a text file containing commands interpreted by a shell. It automates a sequence of commands that you could otherwise type individually in a terminal. Bash is a widely used Unix and Linux command interpreter and scripting language, making it a practical choice for beginner scripts.
This lesson assumes that you can open a terminal, use cd and ls, and create or edit a text file. A shell script is simply a file with commands, but it must use the correct interpreter and permissions when you run it directly.
Create a Script File
Use a terminal text editor to create and save a file. The .sh extension is a conventional way to recognize a shell script, but the extension does not make the file executable.
nano greeting.sh
Enter the following lines in the editor:
#!/bin/bash
echo "Hello, Linux!"
In nano, save the file with Ctrl+O, press Enter to confirm the filename, and exit with Ctrl+X.
Choose the Script Interpreter
The first line is called a shebang. It begins with #! and names the interpreter that should run the file when you execute it directly.
#!/bin/bash
This shebang tells Linux to use Bash. It is important when a script contains Bash-specific syntax, because running the same file with a different shell can produce unexpected syntax or interpreter behavior.
Run a Single Command
The second line uses echo, a command that writes text or expanded values to standard output, normally the terminal.
echo "Hello, Linux!"
The script source is the text stored in greeting.sh:
#!/bin/bash
echo "Hello, Linux!"
When the script runs, the terminal output is the result produced by the command:
Hello, Linux!
The command itself is source code; the greeting is output. They are different things.
Make the Script Executable
Execute permission is file permission that allows a file to be run as a program or script. A newly created text file might not have this permission.
chmod +x greeting.sh
chmod changes file permissions. The +x option adds execute permission. Without it, direct execution can fail with a Permission denied message.
Execute a Script
To run an executable script from the current directory, use a relative path beginning with ./:
./greeting.sh
A relative path describes a location relative to the current working directory. The notation ./ means “the current directory.” Linux normally does not search the current directory when you enter a command name. This prevents an unrelated file in the current directory from being run accidentally.
You can also run the file through Bash:
bash greeting.sh
This method explicitly selects Bash and does not require execute permission on the script file. The Bash program itself must of course be available and executable.
Core Commands and Syntax
Use Multiple Commands
Put commands on separate lines. Bash normally executes them from top to bottom, in the order written.
#!/bin/bash
echo "Current date and time:"
date
Save this as datetime.sh, then run:
chmod +x datetime.sh
./datetime.sh
Example output is:
Current date and time:
Tue Aug 18 12:34:56 UTC 2026
The exact date, time, timezone, and formatting will vary. You can also incorporate the output of date into an echo command with command substitution. Command substitution embeds the output of one command inside another command using $(...).
#!/bin/bash
echo "Current date: $(date)"
Use Variables
A variable is a named value stored for reuse while a script runs. Assign a value with the variable name, an equals sign, and the value. Do not put spaces around the equals sign.
#!/bin/bash
name="Sam"
echo "Hello, $name"
$name is a variable expansion: Bash replaces the reference with the value stored in name. The result is:
Hello, Sam
Double quotes allow variable expansion and preserve spaces inside the resulting value:
message="Linux shell scripts are useful"
echo "$message"
Quote variable expansions when preserving spaces or handling an empty value matters. Double quotes expand variables. Single quotes treat their contents literally, so a variable inside single quotes is not expanded:
name="Sam"
echo "Hello, $name"
echo 'Hello, $name'
The first command prints Hello, Sam; the second prints the literal text Hello, $name.
Read User Input
The Bash builtin read accepts input from standard input and assigns it to a variable. Standard input is commonly the keyboard when a script runs in an interactive terminal.
#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name"
The -p option displays a prompt before waiting. When the user types a name and presses Enter, read stores the entered text in name. The final command expands that value.
Save the file as interactive-greeting.sh and run it:
chmod +x interactive-greeting.sh
./interactive-greeting.sh
Example session:
Enter your name: Sam
Hello, Sam
The script pauses because read is waiting for input. If the script is run with a non-interactive input stream, it may not behave like an interactive prompt.
Common Ways to Run a Shell Script
Troubleshooting
Permission denied with ./script.sh
The file probably lacks execute permission. Add it and try again:
chmod +x script.sh
./script.sh
Command not found when using only the filename
Entering script.sh asks the shell to search its command path. The current directory is normally not included in that search path. Use ./script.sh or run the file through Bash with bash script.sh.
Variable output is empty or literal
Check that the assignment has no spaces around =, that the variable is referenced with $, and that double quotes are used when expansion is required.
name="Sam"
echo "$name"
For example, name = "Sam" is not a valid assignment in Bash because the spaces cause Bash to interpret it as a command.
The script does not wait for a name
Confirm that the script uses read with a variable name, such as read -p "Enter your name: " name. Run it in an interactive terminal so standard input is available.
Unexpected interpreter or syntax behavior
The file may be running under a different shell than intended. Add #!/bin/bash as the first line and use ./script.sh after adding execute permission, or explicitly use bash script.sh.
Exam- and Practice-Relevant Notes
- A shell script is a text file containing shell commands.
- Bash is both a command interpreter and a scripting language.
- The shebang is the first line and identifies the interpreter for direct execution.
- The
.shextension is conventional, not a permission setting. chmod +x fileadds execute permission../fileuses a relative path to run a file in the current directory.bash fileruns the file through Bash without requiring execute permission on the file.- Variable assignments use no spaces around
=. - Variable expansion uses
$variable; double quotes normally preserve spaces and allow expansion. readwaits for standard input and stores entered text in a variable.$(date)is command substitution: it embeds the output ofdatein another command.
Next Steps
After these basics, useful subjects include Linux file structure, file ownership and permissions, and searching text with grep. Later Bash lessons can add conditional logic, script arguments, loops, functions, redirection, exit statuses, and debugging.