What Is A Shell Script
Part of the Fundamentals section of Coddy's Terminal journey — lesson 66 of 82.
So far, you've been typing commands one at a time in the terminal. But what if you need to run the same sequence of commands repeatedly? This is where shell scripts come in.
A shell script is simply a text file containing a series of commands. Instead of typing each command manually, you write them all in a file, and the shell executes them in order. Think of it as a recipe that the computer follows step by step.
Shell scripts typically have a .sh extension and start with a special line called a shebang:
#!/bin/bashThis line tells the system which interpreter should run the script. In this case, it's Bash. Everything after this line is just regular commands, the same ones you've been typing throughout this course.
Shell scripts are powerful because they let you automate repetitive tasks, combine multiple commands into a single action, and share your workflows with others. System administrators use them to manage servers, developers use them to set up projects, and everyday users create them to simplify their daily tasks.
In the upcoming lessons, you'll learn how to create, run, and enhance your own shell scripts with variables, user input, and logic.
Challenge
EasyA shell script called hello.sh has been provided for you. Use the cat command to view its contents and see the structure of a basic shell script.
Notice the shebang line (#!/bin/bash) at the top, followed by regular commands that you already know.
Your output should display:
#!/bin/bash
echo "Hello from a shell script!"
echo "Today is a great day to learn."Hint: Use
cat hello.shto view the script's contents.
Cheat sheet
A shell script is a text file containing a series of commands that the shell executes in order.
Shell scripts typically have a .sh extension and start with a shebang line:
#!/bin/bashThe shebang tells the system which interpreter should run the script (in this case, Bash).
After the shebang, you can write regular shell commands that will be executed sequentially.
Example of a basic shell script:
#!/bin/bash
echo "Hello from a shell script!"
echo "Today is a great day to learn."Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective10Log Analyzer Project
Project OverviewViewing The Log File13Shell Scripting Basics
What Is A Shell ScriptCreate And Run A ScriptVariables In ScriptsUser Input In ScriptsIf StatementFor LoopWhile LoopRecap - Number Guesser2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline