Scripts and Permissions
Write your first .sh script with a shebang, make it executable with chmod +x, and use positional arguments.
- Write a shell script file with the correct shebang line
- Make a script executable with chmod +x
- Run a script using ./script.sh
- Use $1 and $2 to accept positional arguments
- Read basic file permission output from ls -l
Every command you've typed so far disappears when you close the terminal. Scripts fix that — they're files of shell commands that you can save, share, version-control, and run repeatedly. Once you can write a script, you can automate almost anything.
Writing your first script
Create a file called greet.sh:
touch greet.shOpen it in any text editor and add these lines:
#!/bin/bash
echo "Hello, world!"
echo "Today is $(date)"
echo "You are running this as: $(whoami)"The first line, #!/bin/bash, is called the shebang. It tells your operating system which interpreter to use for this file. Without it, the system might use the wrong shell (or none at all). Always start Bash scripts with #!/bin/bash.
File permissions and chmod
The shell protects you from running arbitrary files accidentally. File permissions control who can read, write, or execute a file — and scripts need execute permission before they can run. Try running the script:
./greet.shYou'll likely see:
bash: ./greet.sh: Permission deniedFiles are not executable by default. You need to grant execute permission:
chmod +x greet.sh
./greet.shNow you should see your output. chmod changes the mode (permissions) of a file. +x adds execute permission. The ./ prefix tells the shell to look in the current directory for greet.sh — without it, the shell only searches $PATH.
Reading permissions with ls -l
ls -l greet.shYou'll see output like:
-rwxr-xr-x 1 alice staff 65 Jun 9 2026 greet.shThe first column is the permission string. Break it down:
- rwx r-x r-x
│ │ │ └── other: read and execute
│ │ └──── group: read and execute
│ └──────── owner: read, write, execute
└─────────── file type (- = file, d = directory)r = read, w = write, x = execute, - = not set. After chmod +x, the x bits are turned on.
When the agent's away: When an AI agent writes a script file for you, it will often run chmod +x immediately afterward. This is the standard workflow — create the file, make it executable, run it. The pattern is always the same.
Positional arguments
Scripts become much more useful when they accept input. The shell provides special variables for arguments passed on the command line:
$1— first argument$2— second argument$@— all arguments$0— the script's own name$#— the number of arguments provided
Update greet.sh:
#!/bin/bash
name=$1
city=$2
echo "Hello, $name!"
echo "Greetings from $city."
echo "This script is called: $0"Run it with arguments:
./greet.sh Alice "New York"Output:
Hello, Alice!
Greetings from New York.
This script is called: ./greet.shNotice the quotes around "New York" — without them, New would be $1 and York would be $2. Always quote arguments that contain spaces.
A practical script
Here's a script that creates a project directory with a standard layout:
#!/bin/bash
project_name=$1
if [ -z "$project_name" ]; then
echo "Usage: ./new-project.sh <project-name>"
exit 1
fi
mkdir -p "$project_name/src" "$project_name/tests" "$project_name/docs"
touch "$project_name/README.md"
echo "# $project_name" > "$project_name/README.md"
echo "Created project: $project_name"Don't worry about the if syntax yet — the key takeaway is that scripts combine commands you already know (mkdir, touch, echo) with variables and simple logic to automate repetitive tasks.
Never run scripts you don't understand. A common attack is to trick users into running curl ... | bash with malicious content. Before running any script — especially one from the internet — read it first. chmod +x and ./script.sh are powerful; use them with awareness.
Check your understanding
- 1.What is the purpose of #!/bin/bash at the top of a script?
- 2.In a script called deploy.sh run as ./deploy.sh staging v2.0, what is $2?
- 3.A newly created .sh file is executable by default on Linux and macOS.
Do it yourself
Write and run a script that greets you personally:
touch hello-me.shAdd to the file:
#!/bin/bash
echo "Hello, $(whoami)!"
echo "You are in: $(pwd)"
echo "Your home directory is: $HOME"Then:
chmod +x hello-me.sh
./hello-me.shExtend it: add $1 so you can pass a custom message as an argument.
Where to go next
You can now write, permission, and run shell scripts. Next: pipes and redirects — the |, >, >>, and < operators that let you compose commands into powerful data pipelines.