AI Hints
Home›Linux Tutorial›Your First Scripts
Linux · Lesson 100 of 120

Your First Script

Commands in a file.

⏱ 2 min read▶ Video at 2:14:09🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

A shell script is just a text file, full of commands. Instead of typing them one by one, you save them once, and run the whole file whenever you like. It's how Linux users automate everything.

Your First Script
From the video: Your First Script
  • A text file of commands
  • Save once, run any time
  • Automate everything

hello.sh in nano

Here's our first script, open in nano. The first line, hash bang, bin bash, says which program should run this file. Lines starting with a hash are comments, notes for humans. Then come normal commands, exactly as you'd type them.

nano hello.shbash
#!/bin/bash
# My first script
echo "Hello, $USER!"
echo "Today is $(date +%A)."
echo "You are in $PWD"

Running it

Bash, then the file name, runs the script. To run it on its own, like a real command, give it execute permission with CH mod plus X, and start it with dot slash. Every command in the file runs, from top to bottom.

Terminal
bash hello.sh
Hello, sam!
Today is Monday.
You are in /home/sam
chmod +x hello.sh
./hello.sh
Hello, sam!
Today is Monday.
You are in /home/sam

The shebang line

That first line is called the shebang, from hash and bang. Without it, Linux has to guess which shell should run the file. Always put it first, with nothing before it. And end your script names with dot SH, so people know what they are.

keyvalue
#!/bin/bashrun me with bash
# commentignored by bash
chmod +xmake it runnable
./hello.shrun it

Summary

A script is a file of commands, starting with the shebang. CH mod plus X makes it runnable, and dot slash runs it. Next, giving scripts information, with arguments.

Key takeaways
  • #!/bin/bash
  • chmod +x script.sh
  • ./script.sh
Next up: Script Arguments — $1, $2 and $#