Create And Run A Script
Part of the Fundamentals section of Coddy's Terminal journey — lesson 67 of 82.
Now that you understand what a shell script is, let's create one and run it. The process involves three steps: creating the file, making it executable, and running it.
First, create a new file with a .sh extension. You can use echo with redirection to write the shebang and commands:
echo '#!/bin/bash' > greet.sh
echo 'echo "Hello, World!"' >> greet.shBefore you can run the script, you need to give it execute permission. By default, new files don't have this permission. Use chmod to add it:
chmod +x greet.shNow you can run the script by specifying its path. For a script in your current directory, use ./ before the filename:
./greet.shThis outputs Hello, World!. The ./ tells the shell to look in the current directory rather than searching the PATH. Without it, the shell won't find your script.
You can also run a script without execute permission by calling the interpreter directly:
bash greet.shThis explicitly tells Bash to interpret the file, bypassing the need for execute permission.
Challenge
EasyCreate a shell script called welcome.sh that prints Welcome to scripting! when executed.
Follow these steps:
- Create the script file with the shebang line (
#!/bin/bash) - Add an
echocommand that prints the welcome message - Make the script executable using
chmod - Run the script using
./
Your output should be:
Welcome to scripting!Hint: Use
echo '#!/bin/bash' > welcome.shto create the file with the shebang, thenecho 'echo "Welcome to scripting!"' >> welcome.shto add the command. Make it executable withchmod +x welcome.sh, then run it with./welcome.sh
Cheat sheet
To create and run a shell script, follow these three steps:
1. Create the script file
Create a file with a .sh extension and add the shebang line and commands:
echo '#!/bin/bash' > script.sh
echo 'echo "Hello, World!"' >> script.sh2. Make it executable
Use chmod +x to add execute permission:
chmod +x script.sh3. Run the script
Execute the script using ./ to specify the current directory:
./script.shAlternatively, you can run a script without execute permission by calling the interpreter directly:
bash script.shTry 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